Sergelie
Sergelie

Reputation: 361

Javascript adding class to paragraph if paragraph contains checkbox

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

Answers (2)

David Thomas
David Thomas

Reputation: 253308

I'd suggest selecting the relevant input, and then styling its ancestor:

$('input[type="checkbox"]').closest('p').addClass('stylename');

JS Fiddle demo.

References:

Upvotes: 1

cookie monster
cookie monster

Reputation: 10972

Since that seems to be jQuery...

$("p:has(:checkbox)").addClass("stylename");

DEMO: http://jsfiddle.net/LMB7L/

Upvotes: 2

Related Questions