javascript - Get nested div using document.getElementById -


given following html file,

<html>   <head>      <script>         function initialize() {            document.getelementbyid('id2')         }       </script>   </head>   <body>     <div id="id1">        <div id="id2"></div>      </div>   </body> </html> 

i access inner div tag using

document.getelementbyid 

function in html header, which, although read contrary, not seem work (in safari 6.0.5.), while works access outer tag.

here have fiddle shows works. script needs called after element created.

<body>     <div id="id1">         <div id="id2"></div>     </div>     <script>alert(document.getelementbyid('id2'));</script> </body> 

or:

<body>     <script>         function getelement (id) {             return document.getelementbyid(id);         };     </script>     <div id="id1">         <div id="id2"></div>     </div>     <script>alert(getelement('id2'));</script>  </body> 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -