andrei_1234567890
andrei_1234567890

Reputation: 123

jQuery find element in variable

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....

Upvotes: 9

Views: 22217

Answers (6)

DIEGO CARRASCAL
DIEGO CARRASCAL

Reputation: 2129

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.

Upvotes: 1

Parvez Rahaman
Parvez Rahaman

Reputation: 4397

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);
}

Upvotes: 1

Barmar
Barmar

Reputation: 782785

.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>

Upvotes: 11

KpTheConstructor
KpTheConstructor

Reputation: 3291

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..

Upvotes: 2

terpinmd
terpinmd

Reputation: 1028

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).

Upvotes: 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()

Upvotes: -1

Related Questions