www.webdeveloper.com
Results 1 to 7 of 7

Thread: Why doesn't my getAttribute work ?

  1. #1
    Join Date
    Sep 2012
    Posts
    2

    Why doesn't my getAttribute work ?

    <html>
    <head>
    <script type="text/javascript>
    var a = document.getElementsByTagName("img").getAttribute("src");
    alert(a);
    </script>
    <body>
    <img src="www.google.com" alt="picture"/>
    </body>
    </html>

    I want the browser to alert the src of the picture (www.google.com) but it never works !
    Help much appreciated
    Thanks in advance,
    B.W

  2. #2
    Join Date
    Nov 2010
    Posts
    1,031
    getElementsByTagName returns a collection (even if that collection is only one item, and the contents should be accessed by array notation.

    getAttribute is only really necessary for non-standard attributes. The standard ones (like src) can be accessed directly:
    Code:
    var a = document.getElementsByTagName("img")[0].src;

  3. #3
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,456
    Hi there brucewayne,

    and a warm welcome to these forums.

    Further to what xelawho has said, there are some other reasons for your script failure.

    You are missing a closing double quote here...
    <script type="text/javascript>
    ...which means that the script will never run.

    Although you can set this...
    var a=document.getElementsByTagName('img');
    ...before the page loads, you cannot access the actual elements until after.

    This following will resolve these issues...
    Code:
    <script type="text/javascript">
    
      var a=document.getElementsByTagName('img');
    
    function init(){
    
    for(c=0;c<a.length;c++) {
       alert(a[c].getAttribute('src'));
      }
     }
       window.addEventListener?
       window.addEventListener('load',init,false):
       window.attachEvent('onload',init);
    
    </script>
    
    coothead

  4. #4
    Join Date
    Nov 2010
    Posts
    1,031
    woops. never even looked that far. nice one, coothead

  5. #5
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,456
    Hi there xelawho,

    thanks for the thumbs-up.

    coothead

  6. #6
    Join Date
    Sep 2012
    Posts
    2

    Now what if..

    Now, I don't understand all that window.onload code. So, to make it simpler for me, I'd like to use butttons.

    HTML:

    <img src="www.google.com" alt="picture"/>
    <input type="button" onclick="alertit()"/>

    Javascript:

    <script type="text/javascript">
    function alertit() {
    var a = document.getElementsByTagName("img")[0].getAttribute("src");
    alert(a);
    }

    Why doesn't this work either ?

  7. #7
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,456
    Quote Originally Posted by brucewayne View Post
    Why doesn't this work either ?
    Your code does not have a closing script tag.

    coothead

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles