www.webdeveloper.com
Results 1 to 6 of 6

Thread: .getElementsByClassName wont work

  1. #1
    Join Date
    Apr 2013
    Posts
    36

    .getElementsByClassName wont work


  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    Code:
    <script type="text/javascript">
        function newFunction(){
        var y="hi";
          var x = document.getElementsByClassName("demo")[0];
          x.innerHTML = y;
        }
        window.onload=newFunction;
      </script>
    </head>
    getElementsByClassName() - Returns a node list containing all elements with a specified class (www.w3schools.com/htmldom/dom_methods)
    Last edited by Padonak; 04-27-2013 at 03:18 PM.

  3. #3
    Join Date
    Apr 2013
    Posts
    36
    thank you.
    but i don't understand why you need the [0] ?

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    getElementsByClassName() - Returns a node list containing all elements with a specified class

    the node list mentioned above is an array. this array consists of only one element, but it is still an array, that's why to get the first element of this array we call array_name[index]. the index is 0 because array's indexes start from 0. my english is not good but i hope this makes sense

  5. #5
    Join Date
    Apr 2013
    Posts
    36
    when i try make 2 getelementsbyclassname it wont work

    http://jsbin.com/emasoh/14/edit

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    window.onload=newFunction(); must be window.onload=newFunction;

    var d = getElementsByClassName("demoff")[1]; must be var d = document.getElementsByClassName("demoff")[0];
    Last edited by Padonak; 04-29-2013 at 03:40 PM.

Thread Information

Users Browsing this Thread

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

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