9

I have:

var html_data = '<div class="div1">something</div><div id="this_is_it">Hello!</div>';

How do I check if the html_data var has div this_is_it ?

Trying to check using

if($(html_data).find('#this_is_it').length)

but is not working....

1
  • 1
    Your string isn't properly quoted Commented Jun 3, 2016 at 18:28

6 Answers 6

11

.find() only searches children of the elements it's given, not the top-level elements. So you need to wrap everything in another <div>. Also, you need to fix the quoting problem that causes a syntax error -- if you use double quotes inside the string, use single quotes to delimit it, or vice versa (or escape the quotes).

var html_data = '<div class="div1">something</div><div id="this_is_it">Hello!</div>';
if ($("<div>" + html_data + "</div>").find("#this_is_it").length) {
  console.log("found");
} else {
  console.log("not found");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

Showing error on chrome 'VM210:48 Uncaught SyntaxError: missing ) after argument list'
Sorry, had a typo, missing a +.
2

If you would just like to run a check just use indexOf() .

Example 1 :

var html_data = '<div class="div1">something</div><div id="this_is_it">Hello!</div>';

document.write(html_data.indexOf("this_is_it") > 1 ? true : false)

OR

Example 2:

FYI: Checking if length of this_is_it is greater than 1.

if(html_data.indexOf("this_is_it") > 1){

//do something here

}

Hope this helps..

Comments

1

First as the other posters said your quotations are not right...try this:

var html_data = '<div class="div1">something</div><div id="this_is_it">Hello!</div>';

Secondly you need to use filter in this case. Find looks through children elements only where as filter applies to all the elements.

var one = $(html_data).filter ('#this_is_it').length;

var zero = $(html_data).find('#this_is_it').length;

Your html structure in this case is flat so use filter (or first).

Comments

1

Try this..

var html_data = '<div class="div1">something</div><div id="this_is_it">Hello!</div>';

if(html_data.indexOf("this_is_it")){
    console.log(true);
}else{
    console.log(false);
}

Comments

1

Your problem is in the use of quotations...

it should be structured like:

var html_data = "<div class='div1'>something</div><div id='this_is_it'>Hello!</div>";

or if div1 and this_is_it are variables, then:

var html_data = "<div class="+div1+">something</div><div id="+this_is_it+">Hello!</div>";

Then if($(html_data).find('#this_is_it').length); should give you a result... or if variables: if($(html_data).find('#'+this_is_it).length)

Also terpinmd make a great point in the comments, so for it to work with find() it should be inside a wrapper element. He recommends the use of filter(), which works great. Thank you terpinmd.

2 Comments

find only works if there are child elements. using filter or first is the solution...see my post.
I couldn't tested, but thought it could guide him in the right direction. That's why I write should give you a result..., but then again you are right $(html_data) is not a document or complete DOM. good clarification, I'll included in my answer.
-1

I think you can't use find() to a variable on javascript, you may need to put in your html code and then you get it by find()

1 Comment

jQuery returns an object whose HTML contents come from the variable. Why wouldn't you be able to use find() on it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.