I'm trying to use a link to check a checkbox with jQuery. My HTML is:
<table>
<tr>
<td><input type="checkbox" value="test" /></td>
<td><a class="editbutton" href="#">edit</a></td>
</tr>
</table>
I have been playing with this jquery:
jQuery('.editbutton').click(function($) {
jQuery(this).closest('[type=checkbox]').attr('checked', true);
});
Unfortunately this isn't working. Any ideas?
Use .prop()
instead of .attr()
, Because .prop
is made for setting the properties
. By the way your selector is wrong. .closest()
will traverse up the dom tree.
Please read the following for more reference : .prop() .closest()
Try this,
jQuery('.editbutton').click(function($) {
jQuery(this).closest('td').prev().find('[type=checkbox]').prop('checked', true);
});
Or as @kappa suggested.
jQuery('.editbutton').click(function($) {
jQuery(this).closest('tr').find('[type=checkbox]').prop('checked', true);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With