How to print selected checkbox value after submission
P粉356128676
P粉356128676 2024-04-06 13:07:43
0
2
442

This is my checkbox list and the button I need to submit the data..

<label>Access Locations </label>
     <br />


     <input type="checkbox" name="chk[]" id="check1" class="pl" value="INT" /> dummy1
     <input type="checkbox" name="chk[]" id="check2" class="pl" value="MV" /> dummy2
     <input type="checkbox" name="chk[]" id="check3" class="pl" value="FV" /> dummy3
     <input type="checkbox" name="chk[]" id="check4" class="pl" value="PS" /> dummy4

     <br />
     <h4 name ="checkboxvalues" style="color:green" id="result_checkbox"></h4>  
    
     <input type="hidden" name="employee_id_update" id="employee_id_update" />
     <input onclick="getCheckboxValue()" type="submit" name="insert" id="insert" value="Update" class="btn btn-success" />

I am using the following class when clicking the button..

function getCheckboxValue() {

var l1 = document.getElementById("check1");
var l2 = document.getElementById("check2");
var l3 = document.getElementById("check3");
var l4 = document.getElementById("check4");

 
var res=" "; 
if (l1.checked == true){
  var pl1 = document.getElementById("check1").value;
  res = pl1 + ","; 
} 
else if (l2.checked == true ){
  var pl2 = document.getElementById("check2").value;
  res = res + pl2 + ","; 
}
else if (l3.checked == true){
document.write(res);
  var pl3 = document.getElementById("check3").value;
  res = res + pl3 + ","; 
}
else if (l4.checked == true){
  var pl4 = document.getElementById("check4").value;
  res = res + pl4 + ","; 
}

return document.getElementById("result_checkbox").innerHTML = "You have selected " + res ;

}

From the above method, although I have multiple checkboxes checked, when I click the button, I only see one value in checkboxvalues as shown in the picture.

Can someone tell me how to improve my code so that it displays all values ​​when multiple checkboxes are selected?

If both check1 and check2 are selected the output should be

Output:

You have selected INT,MV

P粉356128676
P粉356128676

reply all(2)
P粉295616170

You are using if-else ladder diagram, which means if one of the conditions is met and executed, the other blocks will be ignored. You can use multiple if blocks to avoid this.

P粉523335026

You are using if else if statements, so once one of them is executed, the rest will not be executed. You can try an if statement for each checkbox and it will do the job.

var res=" "; 
if (l1.checked == true){
  var pl1 = document.getElementById("check1").value;
  res = pl1 + ","; 
} 
if (l2.checked == true ){
  var pl2 = document.getElementById("check2").value;
  res = res + pl2 + ","; 
}
if (l3.checked == true){
document.write(res);
  var pl3 = document.getElementById("check3").value;
  res = res + pl3 + ","; 
}
if (l4.checked == true){
  var pl4 = document.getElementById("check4").value;
  res = res + pl4 + ","; 
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template