JavaScript Display and hide fields of a form after select sap display hide field,sharepoint display form hide field

I have a javascript function that display and hide a div after select. The div contains fields of a form. The div test2 has the fields of the div test1 plus other fields. All the fields are required.

I have 2 problems:

1) When I display test2 and I complete the fields, saving the form is impossible because some fields in test2 are found in test1. This means some fields have the same name.

2) All the fields are required, which means even if I complete the fields in test2 I still can't save because the fields in test1 are not completed and hidden. How can I create a code structure to allow this to save?

function visibilite1(_this) {
  var id = _this.value;
  var divs = document.getElementsByTagName('div');

  for(var no=0;no<divs.length;no++) {
    if (divs[ no].className=='divs') { 
      'divs'
      divs[ no].style.display = "none"; 
    }
  }
  document.getElementById(id).style.display = "block"; 
}


<select name="role" id="role"
onchange="visibilite1(this);hide()" >
  <option value='test1'>Year1</option>
  <option value='test2'>Year2</option>
</select>
<div id="test1" class="divs">
  <label>Name </label>
  <input type="text" name= "name" id= "name" required>  </br>
  <label>User name </label>
  <input type="text" name= "username" id= "username"  required></br>
  <label >Password <em>*</em></label>
  <input type="password"   name= "password" id= "password" required></br>
</div>


<div id="test2" class="divs">
  <label>Name </label>
  <input type="text" name= "name" id= "name" required>  
  <label>User name </label>
  <input type="text" name= "username" id= "username"  required>
  <label>Password <em>*</em></label>
  <input type="password"   name= "password" id= "password" required>
  <input type="text" name= "username" id= "username"  required>
  <label>Adress<em>*</em></label>
  <input type="text"   name= "adress" id= "adress" required>
  <label>Client name</label>
   <?php 
   $sql="select ClientName from claims_follow_up.Client where id 
   !='10001' order by ClientName asc "; 
    $req=mysqli_query($dbc,$sql) or die("Erreur d'execution"); 
    ?> 
   <select name="ClientName" id="ClientName"
   onchange="
   var maVal = document.getElementById('ClientName').value;
   if (maVal == 'Create new client')
   {
   window.open('newClientCreate.php', 'blank', 'scrollbars=yes,    
   resizable=no, top=50, left=155, width=1200, height=700');
   };activer()" required disabled="true" onkeypress="activerSubmit()">
    <option value="">select client </option>
    <?php 
    while($d=mysqli_fetch_array($req)) 
    { 
    echo'<option    
   value="'.$d['ClientName'].'">'.$d['ClientName'].'</option>'; 
     } 
   mysqli_free_result($req); 
   ?>
  <option value="Create new client"  style="color:blue;   
   font-weight:bold">create new client</option>
   </select>
</div>
Answer:1

As you've discovered, elements in a html page should never have the same id and different input elements in a html form should not have the same name.

Assuming you want to stay within the world of vanilla javascript and don't want to use a javascript framework (e.g. angular, ember) you could accomplish this by setting the inner html of elements. This way the "hidden" html isn't present in the html markup.

Something like...

<script>
  function hide () {
    console.log("whatever")
  }

  function visibilite1(_this) {
    var formgroup1 = '<div id="test1" class="divs"><label>Name </label><input type="text" name= "name" id= "name" required>  </br><label>User name </label><input type="text" name= "username" id= "username"  required></br><label >Password <em>*</em></label><input type="password"   name= "password" id= "password" required></br></div>'

    var formgroup2 = '<div id="test2" class="divs"><label>Name </label><input type="text" name= "name" id= "name" required><label>User name </label><input type="text" name= "username" id= "username"  required><label>Password <em>*</em></label><input type="password"   name= "password" id= "password" required><input type="text" name= "username" id= "username"  required><label>Adress<em>*</em></label><input type="text"   name= "adress" id= "adress" required></div>'

    var formtestElement = document.getElementById('formtest'); 

    if (_this.value == "test1") {
      formtestElement.innerHTML = formgroup1
    } else if (_this.value == "test2") {
      formtestElement.innerHTML = formgroup2
    };
  }
</script>

<select name="role" id="role"
  onchange="visibilite1(this);hide()" >
  <option value='test1'>Year1</option>
  <option value='test2'>Year2</option>
</select>

<div id="formtest">
  <div id="test1" class="divs">
    <label>Name </label>
    <input type="text" name="name" id="name" required>  </br>
    <label>User name </label>
    <input type="text" name="username" id="username" required></br>
    <label>Password <em>*</em></label>
    <input type="password" name="password" id="password" required></br>
  </div>
</div>

Update: Edited to show the test1 form by default.

Answer:2

1st thing don't put same id for 2 components, even if its in different divs... 2nd to achieve ur result, when ever u hide the div, disable the input fields corresponding to it and re-enble it when u need

Answer:3

Immediate reaction is that IDs in the document must be unique! - form element names can be the same, but that implies that they are part of a group (ie: within option buttons collection where there's only one possible selection).

Best to keep them all unique, else you need to refer specifically to formName plus elementName.

Answer:4

Before upgrading to Extjs 6 we were using a tagfield component where its displayField was Ext.String.htmlEncode('description') where description was the description field of the store's record. This ...

Before upgrading to Extjs 6 we were using a tagfield component where its displayField was Ext.String.htmlEncode('description') where description was the description field of the store's record. This ...

How can I condition my text to become left-aligned if hits the end of its contained space and has to switch to the next line? Is there something inherent within CSS that can detect it, or does it have ...

How can I condition my text to become left-aligned if hits the end of its contained space and has to switch to the next line? Is there something inherent within CSS that can detect it, or does it have ...

  1. left align center text
  2. left align center html
  3. left align center
  4. left align div
  5. left align div css
  6. left align div bootstrap
  7. left align element css
  8. left align div content
  9. left align div html
  10. left align div elements
  11. left align element
  12. left align div tag
  13. left align element html
  14. align left center css
  15. align left center right on same line
  16. html left align centered text
  17. css left align centered text
  18. align left middle html
  19. align left middle
  20. float left align center

I am using the following regex in conjunction with abide Foundation for validating a password: <form id="setPassword" action="{{ url('changePasswordWithoutToken') }}" method="POST" class="account ...

I am using the following regex in conjunction with abide Foundation for validating a password: <form id="setPassword" action="{{ url('changePasswordWithoutToken') }}" method="POST" class="account ...

  1. regex password validation python
  2. regex password validation javascript
  3. regex password validation c#
  4. regex password validation java
  5. regex password validation codewars
  6. regex password validation no spaces
  7. regex password validation swift
  8. regex password validation 8 characters
  9. regex password validation php
  10. regex password validation python codewars
  11. regex password validation jquery
  12. regex password validation example
  13. regex password validation ruby
  14. regex password validation angular
  15. regex password validation
  16. regex password validation javascript code wars
  17. regex password validation pattern
  18. regex password validation mvc
  19. regex password validation powershell
  20. regex password validation oracle

In my Google Chrome Extension I have: function submitScreenshot() { chrome.tabs.captureVisibleTab( null, function(img) { var xhr = new XMLHttpRequest(), formData = new FormData(); ...

In my Google Chrome Extension I have: function submitScreenshot() { chrome.tabs.captureVisibleTab( null, function(img) { var xhr = new XMLHttpRequest(), formData = new FormData(); ...