0
<DOCTYPE html>
<html>
  <body>
  <h1 >Shankar Mishra</h1>
  <p id="intro">HelloWorld</p>

  <script type="text/javascript">
    var txt=document.getElementById("intro1").innerHTML;
    if(txt==null)
    {
      alert("id doesnot exist");
    }
    document.write(txt);
    alert(txt.nodeName);
  </script>
  </body>
</html>

When I am changing the id name to "intro" it is not returning the node name also please help me out thanks.

BenMorel
  • 34,448
  • 50
  • 182
  • 322

3 Answers3

2

Mistake made is

  txt=document.getElementById("intro1").**innerHTML**

You are checking here node value and saying id doesn't exist Instead use this

  txt=document.getElementById("intro1/intro") //whatever it may be

  if(txt==null){

     alert("id doesn't exist");
  }
  document.write(txt.innerHTML);
  alert(txt.nodeName);
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
  • hey thanks it works but can you tell me the concept of innerHTML.y u have aborted it in document.getElementById("intro1/intro") and introduces it in document.write(txt.innerHTML – Cloud Arch Dec 19 '13 at 07:36
  • Please check my answer in this question http://stackoverflow.com/questions/20604299/what-is-innerhtml-on-input-elements/20604440#20604440 – Shoaib Chikate Dec 19 '13 at 07:39
  • And welcome and I am happy that my answer helped you. – Shoaib Chikate Dec 19 '13 at 07:40
1

I think you want this:

<script type="text/javascript">
  var txt=document.getElementById("intro1");
  if(txt === null){
    alert("id doesnot exist");
  } else {
    document.write(txt.innerHTML);      
    alert(txt.nodeName);
  }
</script>
Dhanu Gurung
  • 8,480
  • 10
  • 47
  • 60
0

Can you try this,

    <DOCTYPE html>
    <html>
    <body>
    <h1 >Shankar Mishra</h1>
    <p id="intro">Hello World!</p>

    <script type="text/javascript">
    var txt=document.getElementById("intro").innerHTML;

    if(txt=="")
    {
    alert("id doesnot exist");
    }
    document.write(txt);
    alert(txt.nodeName);
    </script>
    </body>
    </html>    
Krish R
  • 22,583
  • 7
  • 50
  • 59