0

Possible Duplicate:
Access DIV element from JavaScript

I have a HTML code like :

<div id="headerlist" >
<h3> Enrollment Efficiency</h3>
</div>

I have to access element Enrollment Efficiency

I am trying with :

var headerlist = document.getElementsById('headerlist');
alert("the value of headerlist is "+ headerlist.firstChild);

But its not working.

3
  • 1
    These kind of questions can be easily resolved using Google... Commented Jan 8, 2013 at 8:44
  • The difference is, we're trying to create an archive of useful Q&A's, this won't help anyone in the future. Besides, it has been asked a hundred times over. IMO this is pollution. Commented Jan 8, 2013 at 8:48
  • Check my answer below once. Commented Jan 8, 2013 at 9:05

5 Answers 5

3

Just a typo. It is getElementById() (no plural s there).

var headerlist = document.getElementById('headerlist');

An id should be unique throughout the document, hence only one element should be returned by this functions. If there could be more than one element matched, like with getElementsByName(), there is a plural s.

EDIT

As mentioned by @Cerbrus: The firstChild attribute, just points to first child element, and not to its contents.

To remarks here:

  1. The firstChild in your example is just a whitespace text node. if you want the next non-whitespace node use firstElementChild (supported in most modern browswers).

  2. To display the contents, use something like innerHTML or textContent.

Sign up to request clarification or add additional context in comments.

3 Comments

You might want to mention that firstChild isn't going to get the desired result, like I did.
I think it's also a good idea to mention that firstElementChild is not widely supported yet. Sorry for nagging :P You still have my +1
@Cerbrus Added a link for browser compatibility. Thanks :-)
2

Function getElementByid is singular, not plural.

1 Comment

...contrary to getElementsByClassName which is plural
1

Like others have said, remove the s from getElementsById --> getElementById.

Also, firstChild will return the textNode seperating the div and h3. Try this:

function getFirstChild(element) {
    do { 
        element = element.firstChild;
    } while (element && element.firstChild!== 1);
    return element;
}
// Usage: getFirstChild(headerlist);

Resulting in:

var headerlist = document.getElementById('headerlist');
console.log("the value of headerlist is ", getFirstChild(headerlist).innerHTML);

Comments

0

Probably you want to access the data of inner HTML elements. So here is your code how you can achieve it

CHECK HERE

you can access all child of parent element in following manner.

<!DOCTYPE html>
<html>
<body>
<div id="amu">
<div id="a"> aa </div>
<h1>aaaa </h1>
<h3> bbbb </h3>
</div>

<div onclick="check()"> click Me </div>
</body>
<script>
function check() {
var childNodeArray = document.getElementById('amu').getElementsByTagName('*');
alert(childNodeArray.length);
for (i=0; i < childNodeArray.length; i++)
alert(childNodeArray[i].innerHTML);
}
</script>
</html>

Comments

-1

Try this code in jQuery (library in javascript)

alert($("#headerlist").eq(0).text());

DEMO

5 Comments

Why downvote? jQuery is a library on jquery and to select the element is more efficient
Don't bring a gun to a swordfight, don't answer native JS questions with jQuery, and HECK NO. jQuery is nowhere near as efficient as native JS.
Nothing mentioned about jQuery in the question, they might not know anything about it. Absolutely useless...
Sure but jQuery is more easy to implement and to use I only give another solution with a ajavascript library. @Cerbrus
Easy =/= good. At least, not always. Let people get the basics of JS first, before tossing them a library. Especially if they don't ask for jQuery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.