user3754676
user3754676

Reputation: 321

Detect header or paragraph or bold/italic inside div

How to check if div has header or pre or paragragh tag. Similary font tags have italic or bold tags.

examples of div can be.

<div id="header">
<h1>
<font>
<i></i>
</font>
</h1>
</div>

or if div has 'p' and 'bold' tags inside the font.

 <div id="header">
    <p>
    <font>
    <b></b>
    </font>
    </p>
    </div>

How to detect which element div has . I am doing like.

if($('#headline header'))
{
alert("headline");
}
if($('#headline paragraph'))
{
alert("paragraph");
}

But this is returning both the values.

Here is my div

Div

Upvotes: 1

Views: 180

Answers (2)

Rick Su
Rick Su

Reputation: 16440

You should make use of .has()

var header = $("#header");

if(header.has("h1,h2,h3,h4,h5,h6").length) {
   alert("#header div has header");
}

if(header.has("p").length) {
   alert("#header div has paragraph");
}

Upvotes: 4

Sebastian Simon
Sebastian Simon

Reputation: 19525

$('Anything that you put inside this string as a parameter for the jQuery function') will return an object whose truthiness is true. Use $('').length instead. This will return either 0 which in an if statement will be evaluated as false or any other number which is considered true.

Also header and paragraph are not the tag names you’re looking for. You probably meant h1 and p and then again #header instead of #headline.

Upvotes: 2

Related Questions