Search code examples
javascripthtmljqueryradio-button

When div is clicked, check corresponding radio input jquery


Here's my problem, I want an entire div to be click able, when clicked I need the radio button contained in the div to be checked, so the div acts as a radio itself. Here's what I thought I could use;

$('#content').click(function(e) { $('input:radio[name="id_"]').prop('checked', true); });

But this is not selecting the relative radio inside the div. I think I can use a this selector, am I right?


Solution

  • building on Deif's solution this will toggle the checked status when clicking on the div

    fiddle

    <div id="content">
        Some content
        <input type="radio" id="radio1" value="test" />
    </div>
    
    <script type="text/javascript">
    $('#content').click(function () {    
       var val =  $(this).find('input:radio').prop('checked')?false:true;
       $(this).find('input:radio').prop('checked', val);
    });
    </script>