Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting an element from with a div using dom

Status
Not open for further replies.

stinkybee

Programmer
Joined
May 15, 2001
Messages
218
Location
GB
I am trying to get information from html elements within my page that are enclosed in a div. Here is some example html:

Code:
<div id="div1">
   <input name="input" value="whatever">
</div>
<div id="div2">
   <input name="input" value="more stuff">
</div>

so what I need is to get the value of the input based on the "name" attribute of the input and the "id" attribute of the div.
basically something like this

Code:
document.getElementById('div1').getElementsByName('input').value

obviously this doesn't work but I think it gets over what I want. I need a method of getting all of the elements named "input" within the div with the id of "div1
 
obviously this doesn't work
The reason for this is that you are trying to access the value from a method that returns an array collection. Your array points to a collection of elements, so when you want to extract their values you need to specify which element in the array you are pulling from. In the example you posted you only have one input element in the div, so had you done this instead it would have worked:
Code:
document.getElementById('div1').getElementsByName('input')[!][0][/!].value

So, you can set the collection equal to a variable and then loop thru that collection to pull each value:
Code:
var a = document.getElementById('div1').getElementsByName('input')
for (i = 0; i < a.length; i++) {
   alert(a[i].value);
}

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Thanks for the replies.

kaht: your reply doesn't work, not sure if you were confusing getElementsByName with getElementsByTagName. My fault for giving the "input" tag the "name" input.
It does work with getElementsByTagName however.

I hadn't thought of doing it through tag names as I wanted to access the name of the element directly ignoring element type. This answer works great for me now but just wondering whether there was a way of directly accessing an element that is within a div through the name attribute.

Thanks again for the replies.
 
mine's better. can you always be sure your input element, cleverly named "input", will be the first input element in the div?

i wouldn't trust kaht as far as i can throw him.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Cory's a bum, he likes the mets.
puke.gif


-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top