SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementsByTagName()

    I have just recently begun to put getElementsByTagName() to good use! Testing different things! I wrote the following script and came upon a question! I have used this method with tag names getElementsByTagName('div/a/span'), and I guess getElementsByTagName('*') is all the tags on the page! I wondered if you could reference them in there order on the page like this getElementsByTagName(1) this being the second tag on the page the <head>? Can this be done thanx?

    Code:
    <html>
    	<head>
    		<script language="Javascript">
    function reference(unique,id,clsName){
    this.unique=unique;
    this.id=id;
    this.clsName=clsName;
    }
    data=new Array();
    window.onload=function alt(){
    for(var i=0;i<document.getElementsByTagName('div').length;i++){
    data[i]=new reference(document.getElementsByTagName('div')[i].tagName+i,
    document.getElementsByTagName('div')[i].id,
    document.getElementsByTagName('div')[i].className);
    }
    }
    		</script>
    	</head>
    	<body>
    		<div id="div_0" class="box_0" onclick="alert(data[0].unique)">text</div>
    		<div id="div_1" class="box_1">text</div>
    		<div id="div_2" class="box_2">text</div>
    	</body>
    </html>

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Yes. For example, getElementsByTagName(0) should return the <html> element.

  3. #3
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    something like
    Code:
    <html>
    <body>
    <div></div>
    <script>x=document.getElementsByTagName('*').length
    for(i=0;i<x;i++) {
    alert(tagType=document.getElementsByTagName('*')[i].nodeName)}
    </script>
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just replaced nodeName with innerHTML and that gives interesting results too.........

    for some reason it shows the start of TagName('*')[0].innerHTML as <html></html>
    Weird.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  5. #5
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Also, I get different results in IE, NS and Opera....
    IE reads a <head> and <title> in the above script
    NS reads a <head>
    only Opera reads as written

    If you use valid HTML;
    Code:
    <html>
    <head>
    <title>Tags</title>
    </head>
    <body>
    <div>A Division</div>
    <script type="text/javascript">
    var x=document.getElementsByTagName('*')
    for(i=0;i<x.length;i++) {
    alert("tagType "+i+"="+x[i].nodeName)}
    </script>
    </body>
    </html>
    everything is fine
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •