Reputation: 943
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?
Upvotes: 0
Views: 8191
Reputation: 3973
Yes you can use the this
selector. I have made a quick jsfiddle to show you an example.
Upvotes: 3
Reputation: 5578
Try with this:
$('div').click(function(){
if( $('div').find('input:checkbox').prop("checked") == true){
$('div').find('input:checkbox').prop("checked", false);
}
else{
$('div').find('input:checkbox').prop("checked", true);
}
});
Upvotes: 1
Reputation: 2841
building on Deif's solution this will toggle the checked status when clicking on the div
<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>
Upvotes: 1
Reputation: 2588
You don't give any code, so I guess:
<div class="content">
<input type="radio" name="foo">
</div>
<div class="content">
<input type="radio" name="foo">
</div>
<div class="content">
<input type="radio" name="foo">
</div>
.content {
float: left;
width: 100px;
padding-top: 100px;
background-color: #eee;
margin-left: 10px;
}
$('.content').click(function(){
$(this).find('input[type=radio]').prop('checked', true);
})
Upvotes: 4
Reputation: 2229
$('#content').click(function(){
$(this).children('radio').attr('checked','checked')
})
Upvotes: 2
Reputation: 998
This should do it.
$('input:radio[name*="id_"]'), assuming the name starts with id_
And yes you can use this
. Use it to filter down its children like:
$(this).children('input:radio[name*=id_]').prop("checked", true)
The key is using name*=id_
This means select element whose name starts with id_
. Isn't that what you wanted ?
Upvotes: 2