I’m a JS beginner. I posted this question because I’ve been trying to find a solution for 2 hours (it’s 3:00am and I’m dying to go to bed). So thanks for understanding.
I want to get the child node of the tagname div. Then alert the name (txtPhone).
<div class="formfield">
<input id="txtPhone" name="txtPhone" />
So:
alert(document.getElementsByTagName("div")...?);
thanks a lot!
Using just JavaScript, the property you’re after is firstChild.
To alert the name of the element:
Note that you’re using getElementsByTagName which returns an array of all div elements – in my above example, I’m accessing the first element from this set, so this will only return the correct link if your div is the first one on the page.
To get around this, I’d suggest giving the div tag you want to target an id, and then using getElementById(“id”) instead:
and
If you actually want to target a group of elements, I’d suggest using jQuery’s selectors to target the specific elements, such as
$(".formfield").firstChild.name