60
<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 can i change the attribute "hidden" to false in Jquery when the checkbox is checked or not checked.

3
  • possible duplicate of How to enable button when checkbox clicked in jquery? Commented Jun 12, 2015 at 17:46
  • @user3314813, if none of the answers worked or you are still facing trouble, let me know so I can help Commented Jun 12, 2015 at 22:27
  • Using .prop('hidden', false) and .prop('hidden', true) worked for me while .removeAttr('hidden') didn't. Commented Jul 13, 2019 at 2:53

4 Answers 4

71
$(':checkbox').change(function(){
    $('#delete').removeAttr('hidden');
});

Note, thanks to tip by A.Wolff, you should use removeAttr instead of setting to false. When set to false, the element will still be hidden. Therefore, removing is more effective.

Sign up to request clarification or add additional context in comments.

5 Comments

Hidden is a boolean attribute, you need to remove it, not setting it to 'false'. Or use .prop('hidden', false);
@A.Wolff, do you mean attr wont work or something else?
@A.Wolff, according to sitepoint.com/web-foundations/hidden-html-attribute value is 'true' or 'false'
Looks like wrong interpretation,see e.g setting it to false, element still hidden: jsfiddle.net/nyq35m7x
@A.Wolff, I see. Thanks so much for pointing out. Updated my answer giving you credit. :) thanks again
69

You can use jquery attr method

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

4 Comments

Put the change handler in your answer so it will be complete answer. Then, I will delete mine
why not .prop("hidden",true);?
@Alex78191 hidden is an attribute, not a property. w3schools.com/tags/att_hidden.asp
18

A. Wolff was leading you in the right direction. There are several attributes where you should not be setting a string value. You must toggle it with a boolean true or false.

.attr("hidden", false) will remove the attribute the same as using .removeAttr("hidden").

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

You should not be setting hidden, checked, selected, or several others to any string value to toggle it.

Comments

12

Use prop() for updating the hidden property, and change() for handling 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.