javascript - Inner Text not working for Paragraph and Heading HTML Elements -


i'm newbie javascript, tried below code, works fine <div> element not <p> , <h1> elements

<script type="text/javascript">     function printtext(){         document.getelementbyid('heading').innertext = 'hello world';     } </script>  <body>     <div id="heading"></div> // works     <h1 id="heading"></h1> // not working     <p id="heading"></p> // not working      <button type="button" onclick="printtext()">submit</button> </body> 

when use document.getelementbyid('heading').innerhtml= 'hello world'; <p> , <h1> elements above script works(using innerhtml instead of innertext)

why innertext property not working <p> , <h1> elements?

first suggestion don't ever put same ids on multiple elements in same page.

why?

because when document.getelementbyid() browser lookup stops when finds first element of id.

second suggestion is:

change

innertext 

to.

textcontent 

innertext won't work cross browser. better use standard way put text textcontent.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -