Reputation: 361
So, I am trying to add a class to a paragraph only if the paragraph contains a checkbox. Here is my code (faulty obviously).
$(window).load(function() {
$('p').each(function(){
var p = element.parentNode;
if ( $(this).val() != '')
if ( $(this).val().indexOf('checkbox')>=0)
{
p.className = p.className + "stylename";
}
else {alert ('the if function works');} // test
}
);})
Upvotes: 0
Views: 109
Reputation: 253308
I'd suggest selecting the relevant input, and then styling its ancestor:
$('input[type="checkbox"]').closest('p').addClass('stylename');
References:
Upvotes: 1
Reputation: 10972
Since that seems to be jQuery...
$("p:has(:checkbox)").addClass("stylename");
DEMO: http://jsfiddle.net/LMB7L/
Upvotes: 2