How to change hidden attribute in jquery

<td> <input id="check" type="checkbox" name="del_attachment_id[]" value="<?php echo $attachment['link'];?>"> </td> <td id="delete" hidden="true"> the file will be deleted from the newsletter </td> 

I want to know how to change the "hidden" attribute to false in jQuery when the checkbox is checked or not checked.

+5
source share
4 answers
 $(':checkbox').change(function(){ $('#delete').removeAttr('hidden'); }); 

Note that thanks to the prompt A.Wolff you should use removeAttr instead of setting to false. If set to false, the item will still be hidden. Therefore, removal is more efficient.

+12
source

a. Wolf led you in the right direction. There are several attributes in which you should not set a string value. You must switch it using boolean true or false .

.attr("hidden", false) will remove the attribute in the same way as with .removeAttr("hidden") .

.attr("hidden", "false") invalid and the tag remains hidden.

You should not set hidden , checked , selected or several others for any string value to switch.

+7
source

You can use jquery attr method

 $("#delete").attr("hidden","true"); 
+5
source

Use prop() to update the hidden property and change() to handle the change event.

 $('#check').change(function() { $("#delete").prop("hidden", !this.checked); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table> <tr> <td> <input id="check" type="checkbox" name="del_attachment_id[]" value="<?php echo $attachment['link'];?>"> </td> <td id="delete" hidden="true"> the file will be deleted from the newsletter </td> </tr> </table> 
+1
source

All Articles