onerkript onerkript - 6 months ago 18
jQuery Question

Check input radio on image click

Here's my HTML:

<img src="img.jpg" class="img-ms-form">
<div class="radio">
<label class="radio"><input type="radio" name="optradio-1" required>Option 1</label>
</div>


I'm trying to check the radio button if the image is clicked. Here's what I've tried:

$('.img-ms-form').click(function(){
$(this).next('input[type=radio]').prop('checked', true);
});


But this doesn't seem to work, what am I doing wrong?

Answer

Use .next() and .find() as shown :-

$('.img-ms-form').click(function(){
  $(this).next('div.radio').find('input[type="radio"]').prop('checked', true);
});
Comments