Get elements (div) name with javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d3vkit
    New Member
    • Nov 2006
    • 34

    Get elements (div) name with javascript?

    Okay I bet this is the easiest thing to figure out, but I've tried EVERYTHING to no avail. Maybe it's just not possible but I don't see why not.

    I have a php page which is grabbing content from mysql and looping with while to display it. Each entry has a unique id, which I am setting as the name (the id is already taken in my script).

    I then have this code:
    Code:
    window.onload = function() {
    	alert(document.getElementById('holder0').name);
    }
    So I can see the name alerted to me. This yields undefined. 'holder0' exists and has a name.

    Am I missing something? Why can't I get the elements name with getelbyid?

    Thanks in advance.
  • b1randon
    Recognized Expert New Member
    • Dec 2006
    • 171

    #2
    Originally posted by d3vkit
    Okay I bet this is the easiest thing to figure out, but I've tried EVERYTHING to no avail. Maybe it's just not possible but I don't see why not.

    I have a php page which is grabbing content from mysql and looping with while to display it. Each entry has a unique id, which I am setting as the name (the id is already taken in my script).

    I then have this code:
    Code:
    window.onload = function() {
    	alert(document.getElementById('holder0').name);
    }
    So I can see the name alerted to me. This yields undefined. 'holder0' exists and has a name.

    Am I missing something? Why can't I get the elements name with getelbyid?

    Thanks in advance.
    Please post a snippet from your source with holder0.

    Comment

    • benhunt
      New Member
      • Dec 2006
      • 5

      #3
      The problem is that Divs don't normally have names. You would be able to read the "name" of a form element that way, I think.

      But for any custom attribute, you need to use the getAttribute method, like this:
      Code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      	<title>Untitled</title>
      	<script type="text/javascript">
      	<!--
      		window.onload = function() {
      			alert(document.getElementById("div1").getAttribute("name"));
      		}
      	// -->
      	</script>
      </head>
      
      <body>
      
      <div id="div1" name="Betty">I am div 1</div>
      
      </body>
      </html>

      Comment

      • AricC
        Recognized Expert Top Contributor
        • Oct 2006
        • 1885

        #4
        If ^^ doesn't help let us know what holder0 is

        Comment

        • matttzu
          New Member
          • Apr 2009
          • 1

          #5
          If you use getElementById you must have an id, if you only have a name use getElementByName.

          hope this helped.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            there is no standards-compliant method called:

            Code:
            document.getElementByName()
            only:

            Code:
            document.getElementsByName()
            note the s for Elements ... this method returns a collection of nodes with a name since just an id has to be unique within a document, but for names this is not required.

            kind regards

            Comment

            Working...