I am trying that when i click on checkbox hidden field of "present" id become visible if i uncheck the checkbox then hidden field of "absent" id become visible.But when i click on checkbox only first row of table is affected while other rows didn't affect when checkbox is checked or unchecked. Following is the html code
<table class="table table-bordered">
<tr>
<td><strong> Name</strong></td>
<td><strong>Email</strong></td>
<td><strong>Phone</strong></td>
<td><strong>Cnic</strong></td>
<td><strong>Attendence</strong></td>
<td><strong>Status</strong></td>
</tr>
<?php foreach($user as $USER){?>
<form method="post" action="">
<tr>
<td>
<input type="text" name="name" value="<?=$USER->name;?>" readonly>
<input type="hidden" name="u_id" value="<?=$USER->u_id;?>">
</td>
<td>
<input type="text" name="email" value="<?=$USER->email;?>" readonly>
</td>
<td>
<input type="text" name="phone" value="<?=$USER->phone;?>" readonly>
</td>
<td>
<input type="text" name="cnic" value="<?=$USER->cnic;?>" readonly>
</td>
<td style="text-align: center">
<input type="checkbox"name="checkbox"id="checkbox" onclick="visibility()">
</td>
<td>
<span style="visibility: hidden; color: green;"><input type="text" name="att" value="Present" id="present"></span>
<span style="visibility: hidden; color: red;"><input type="text" name="att" value="Absent" id="absent"></span>
</td>
</tr>
<?php }?>
</form>
</table>
And following is the JavaScript code
<script type="text/javascript">
function visibility() {
if (document.getElementById("checkbox") . checked == true){
document.getElementById("present").style.visibility ="visible";
document.getElementById("absent").style.visibility = "hidden";
}else{
document.getElementById("absent").style.visibility = "visible";
document.getElementById("present").style.visibility = "hidden";
}
}
</script>
I would do this
<span style="visibility: hidden; color: green;"><input type="text" name="att" value="Present" class="present"></span>
<span style="visibility: hidden; color: red;"><input type="text" name="att" value="Absent" class="absent"></span>
<script type="text/javascript">
function visibility() {
if (document.getElementsByClassName("checkbox") . checked == true){
document.getElementsByClassName("present").style.visibility ="visible";
document.getElementsByClassName("absent").style.visibility = "hidden";
}else{
document.getElementsByClassName("absent").style.visibility = "visible";
document.getElementsByClassName("present").style.visibility = "hidden";
}
}
</script>
Try the following method:
While rendering your form, also send user id as parameter to visibility
method.
onclick="visibility(<?=$USER->u_id;?>)"
Set id of absent & present fields by appending user id to the id attribute.
<span style="visibility: hidden; color: green;"><input type="text" name="att" value="Present" id="present<?=$USER->u_id;?>"></span> <span style="visibility: hidden; color: red;"><input type="text" name="att" value="Absent" id="absent<?=$USER->u_id;?>"></span>
Similarly set id of checkbox.
<input type="checkbox"name="checkbox"id="checkbox<?=$USER->u_id;?>" onclick="visibility(<?=$USER->u_id;?>)">
Use the user id sent as parameter to visibility
method to identify the id of element.
function visibility(id) { if (document.getElementById("checkbox"+id) . checked == true){ document.getElementById("present"+id).style.visibility ="visible"; document.getElementById("absent"+id).style.visibility = "hidden"; }else{ document.getElementById("absent"+id).style.visibility = "visible"; document.getElementById("present"+id).style.visibility = "hidden"; } }
<script type="text/javascript"> function visibility(id) { if (document.getElementById("checkbox"+id) . checked == true){ document.getElementById("present"+id).style.visibility ="visible"; document.getElementById("absent"+id).style.visibility = "hidden"; }else{ document.getElementById("absent"+id).style.visibility = "visible"; document.getElementById("present"+id).style.visibility = "hidden"; } } </script>
<table class="table table-bordered"> <tr> <td><strong> Name</strong></td> <td><strong>Email</strong></td> <td><strong>Phone</strong></td> <td><strong>Cnic</strong></td> <td><strong>Attendence</strong></td> <td><strong>Status</strong></td> </tr> <?php foreach($user as $USER){?> <form method="post" action=""> <tr> <td> <input type="text" name="name" value="<?=$USER->name;?>" readonly> <input type="hidden" name="u_id" value="<?=$USER->u_id;?>"> </td> <td> <input type="text" name="email" value="<?=$USER->email;?>" readonly> </td> <td> <input type="text" name="phone" value="<?=$USER->phone;?>" readonly> </td> <td> <input type="text" name="cnic" value="<?=$USER->cnic;?>" readonly> </td> <td style="text-align: center"> <input type="checkbox"name="checkbox"id="checkbox<?=$USER->u_id;?>" onclick="visibility(<?=$USER->u_id;?>)"> </td> <td> <span style="visibility: hidden; color: green;"><input type="text" name="att" value="Present" id="present<?=$USER->u_id;?>"></span> <span style="visibility: hidden; color: red;"><input type="text" name="att" value="Absent" id="absent<?=$USER->u_id;?>"></span> </td> </tr> <?php }?> </form> </table>
id must be unique.
add just parameter in your function eg onclick="visibility(1)" --for first row and pass 2 insted of 1 and so on also do this for your id id="present_1" and id="absent_1" and so on and the js function is as below
function visibility(no) {
if (document.getElementById("checkbox") . checked == true){
document.getElementById("present_"+no).style.visibility ="visible";
document.getElementById("absent_"+no).style.visibility = "hidden";
}else{
document.getElementById("absent_"+no).style.visibility = "visible";
document.getElementById("present_"+no).style.visibility = "hidden";
}
}
Hope its work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.