tags:

views:

42

answers:

5

Here it is DOM structure:

<div id="some">
 <a href="#" style="color:red;">NOTHIS</a>
 <a href="#" style="color:red;">NOTHIS</a>
 <h3 class="myclass"><a href="#" style="color:red;">HELLO</a></h3>
</div>

How can I get the value of HELLO in javascript?

EDIT: Forgot, I have other anchor tags inside 'some', so I want strictly the anchor tag inside the h3's

EDIT2: Got it:

var n = document.getElementById('some').getElementsByTagName('h3')[0].getElementsByTagName('a')[0].innerHTML;

Thanks all!

+5  A: 

var linkText = document.getElementById('some').getElementsByTagName('a')[0].innerHTML;

or if you have jQuery

var linkText = $('#some').find('a').html();
Josiah Ruddell
hopefully he's not using bringing in an entire library specifically for this one thing though.
castis
I don't think bringing in jquery, or at least sizzle, is a bad idea if you need to traverse the DOM only in a few places. DOM traversing with the DOM interface is always very brittle, any changes to the HTML will require a change to the code. Use getElementsByTagname if you don't mind brittle code.
Juan Mendes
A: 

Propagate down the DOM from your ID.

var s = document.getElementById('some').getElementsByTagName('h3')[0].getElementsByTagName('a')[0].innerHTML;

I would put an ID on the a myself.

Dustin Laine
+1  A: 
var anchor = document.getElementById('some').getElementsByTagName('a')[0],
    yourText = anchor.innerText || anchor.textContent;

It's cross-browser, too. http://www.quirksmode.org/dom/w3c_html.html

Matt Ball
A: 
var shouldEqualHello = document.getElementById('some').getElementsByTagName('h3')[0].getElementsByTagName('a')[0].innerHTML;

edit: fixed

castis
context.getElementsByTagName returns an array.
Josiah Ruddell
hence the [0] afterwards
castis
look at your selector with the 'h3'
Josiah Ruddell
oops, right you are sir!
castis
A: 

to get to a single dom element with javascript, you need a way to uniquely identify it. the ideal approach is to give your element a unique id.

<a id="myAnchor" href="#" style="color:red;">HELLO</a>

then you can directly obtain a reference in script.

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

or if you are guaranteed that your element is the only anchor element within the "some" id you can do

var someDiv = document.getElementById('some');
var anchors = someDiv.getElementsByTagName('a'); // returns a list of anchor elements
var myAnchor = anchors[0]; // get the first element in the list

but since that's not the case you'll have to pick your way down through the dom some more.

var someDiv = document.getElementById('some');
var headers = someDiv.getElementsByTagName('h3');
var myH3 = headers[0];
var anchors = myH3 .getElementsByTagName('a'); // returns a list of anchor elements
var myAnchor = anchors[0]; // get the first element in the list

from there you can see the stuff between the tags with

alert(myAnchor.innerHTML);

or

alert(myAnchor.firstChild.nodeValue);

or some other method already mentioned here.

lincolnk