javascript - jQuery find if div with id=X exists inside a DOM string - Stack Overflow

I'm trying to check if a div with an id exists in a string holding some html, so I'm looking

I'm trying to check if a div with an id exists in a string holding some html, so I'm looking for a function that returns true or false.

There's a function hasClass that checks if a div has a certain class

$('#mydiv').hasClass('bar')

I guess what I'm looking for is something like

var mystring = "some string with html";

mystring.hasId('lookingforthisid');

How can I check this?

I'm trying to check if a div with an id exists in a string holding some html, so I'm looking for a function that returns true or false.

There's a function hasClass that checks if a div has a certain class

$('#mydiv').hasClass('bar')

I guess what I'm looking for is something like

var mystring = "some string with html";

mystring.hasId('lookingforthisid');

How can I check this?

Share Improve this question edited Apr 11, 2011 at 15:46 bluish 27.4k28 gold badges125 silver badges184 bronze badges asked Nov 11, 2010 at 11:12 sarimotosarimoto 792 silver badges3 bronze badges 1
  • any reason for not using document.getElementById? – jebberwocky Commented Nov 11, 2010 at 13:29
Add a ment  | 

3 Answers 3

Reset to default 2

Turn it to a jquery object and use the .find() method to locate the element you want

var mystring = "some string with html";

var $mystring = $('<div>'  + mystring + '</div>');

alert( $mystring.find('#lookingforthisid').length );

if it has .length greater than 0 then it exists..


Improvement / Generalization

If you want to make a general function to check if strings contain some jquery selector you can do this

String.prototype.hasSelector = function( aSelector ){
    return ($( '<div>' + this.toString() +'</div>').find( aSelector ).length>0) ? true : false;
};

and use it like this

var mystring = 'test <div id="someid">text in ID</div> outer <div class="someclass">text in class</div> ';

alert( mystring.hasSelector('#someid') );
alert( mystring.hasSelector('.someclass') );

Live example at http://www.jsfiddle/gaby/Ajp7K/

jquery find if div with id=X exists

Use the length:

if($('#mydiv').length > 0){
  // element exists
}
else{
 // element does not exist
}

You can convert it to a function like this:

function element_exists(id){
    if($(id).length > 0){
      return true;
    }

    return false;
}

And use like:

if (element_exists('#some_id')){
  // element exists
}
else{
  // element doesn't exists
}

If you want to check if an element with certain class exists, you can use hasClass method instead.

If you want to check if an element with certain piece of text exists, you can use the :contains filter selector instead. Here is an example:

alert($('#element_id:contains("some text")').length);

find if element exists within a string of HTML:

if($("div#mydiv", mystring).length >= 1) {
    return true;
} else {
    return false;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745425698a4627164.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信