In JavaScript how to check html element existing or not? - Stack Overflow

I want to check whether a html element exists in the page. var e = document.getElementById("test-n

I want to check whether a html element exists in the page.

var e = document.getElementById("test-node");
if(e == null)
{
 // do something
}

But the above code is not working. How can I check this element is existing or not? Any suggestions will be appreciated.

I want to check whether a html element exists in the page.

var e = document.getElementById("test-node");
if(e == null)
{
 // do something
}

But the above code is not working. How can I check this element is existing or not? Any suggestions will be appreciated.

Share Improve this question edited Feb 7, 2014 at 18:32 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked Oct 29, 2013 at 9:58 SukeshiniSukeshini 1,2612 gold badges24 silver badges49 bronze badges 9
  • It should work. Can you make a fiddle demonstrating the problem? – Barmar Commented Oct 29, 2013 at 10:01
  • You can do it very simple by using jQuery, is this library an option in you project? – Martin Commented Oct 29, 2013 at 10:01
  • use the entire jquery just for this test? LMAO – Fez Vrasta Commented Oct 29, 2013 at 10:01
  • @Barmar : This element is a dropdown box. It has weveral options to select. Only for the existing users will be able to see the dropdown box. For new users, they cannot see it. When the user logged in I want to do some settings according to this option selected. For new users also I need to do some settings. To identify the new user I'm using this dropdown box's existence. – Sukeshini Commented Oct 29, 2013 at 10:05
  • How are you preventing new users from seeing it? If you're just hiding it with CSS, the element still exists. – Barmar Commented Oct 29, 2013 at 10:10
 |  Show 4 more ments

6 Answers 6

Reset to default 9

This will work:

if (document.getElementById("test-node")) {

   // exists

}

you can check it directly inside if condition.

try this code

if(document.getElementById("test-node"))
{
  // exists
}
else
{
//doesnot exists
}

fiddler: http://jsfiddle/codingsolver/rPHTb/

Using the Node.contains DOM API, you can check for the presence of any element in the page (currently in the DOM) quite easily:

document.contains(someReferenceToADomElement);

Try This

if (the_object.hasOwnProperty("ElementName"))  
  {  
   //some action

  }

HTML

<div id="test"></div>

JS

var e = document.getElementById('test');
if(e){
    alert("we have the test div");
} else {
    alert("we don't have the test div");
}

var e2 = document.getElementById('test2');
if(e2){
    alert("we have the test2 div");
} else {
    alert("we don't have the test2 div");
}

http://jsfiddle/7tgvb/1/

Try this.

var e = document.getElementById("test-node");
if(e)
{
  // do something
}

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

相关推荐

  • In JavaScript how to check html element existing or not? - Stack Overflow

    I want to check whether a html element exists in the page. var e = document.getElementById("test-n

    7天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信