PDA

View Full Version : Simply Printing IDs values


tkmoney
05-19-2005, 08:58 PM
Ok I have a html file like this :

<html>
<head>

<script type="text/javascript">

document.write(document.getElementsByName("unit").id)

</script>

</head>

<body>

<div id="1" name="unit">content</div>
<div id="2" name="unit">content</div>
<div id="3" name="unit">content</div>
<div id="4" name="unit">content</div>

</body>

</html>



Im just trying to print the id values. Its printing "undefined" right now. Im sure this is simple to do.

hemebond
05-19-2005, 11:10 PM
document.getElementsByName('unit') will return an array.

tkmoney
05-19-2005, 11:19 PM
document.getElementsByName('unit') will return an array.


That outputs "[object HTMLCollection]"

hemebond
05-19-2005, 11:31 PM
That outputs "[object HTMLCollection]"Which is an array.document.getElementsByName('unit')[0].id

tkmoney
05-19-2005, 11:36 PM
Which is an array.document.getElementsByName('unit')[0].id



That doesnt output anything.

tkmoney
05-20-2005, 12:17 AM
Please god someone help me, Iv spent all day on this.

Harry Armadillo
05-20-2005, 12:30 AM
It's undefined because you are trying to work with an object before it exist - stick your script fragment after the unit divs or do something like<html>
<head>

</head>

<body>

<div id="bob" name="unit">content</div>
<div id="jake" name="unit">content</div>
<div id="frank" name="unit">content</div>
<div id="guido" name="unit">content</div>

<input type=button onclick='alert(document.getElementsByName("unit")[0].id)' value='unit 0'><br>
<input type=button onclick='alert(document.getElementsByName("unit")[1].id)' value='unit 1'><br>
<input type=button onclick='alert(document.getElementsByName("unit")[2].id)' value='unit 2'><br>
<input type=button onclick='alert(document.getElementsByName("unit")[3].id)' value='unit 3'><br>
</body>

</html>

tkmoney
05-20-2005, 12:59 AM
It's undefined because you are trying to work with an object before it exist - stick your script fragment after the unit divs or do something like<html>
<head>

</head>

<body>

<div id="bob" name="unit">content</div>
<div id="jake" name="unit">content</div>
<div id="frank" name="unit">content</div>
<div id="guido" name="unit">content</div>

<input type=button onclick='alert(document.getElementsByName("unit")[0].id)' value='unit 0'><br>
<input type=button onclick='alert(document.getElementsByName("unit")[1].id)' value='unit 1'><br>
<input type=button onclick='alert(document.getElementsByName("unit")[2].id)' value='unit 2'><br>
<input type=button onclick='alert(document.getElementsByName("unit")[3].id)' value='unit 3'><br>
</body>

</html>


Thanks! but what if I wanted to list all of the Id values on one page?

Harry Armadillo
05-20-2005, 02:01 AM
I'm not into guessing such things, but what if you 'wanted to list all of the Id values'? I suppose that you'd either write a function to do so, or you'd ask how to write such a function, or (as a last resort, I hope) you'd ask someone to write it for you. If you were to choose one of the latter two options, I'd like to think you'd fully describe the goal/problem and that you'd do enough background work to understand the solutions offered.

Some good reading here: http://www.javascriptkit.com/javaindex.shtml
An important thread here: http://www.codingforums.com/showthread.php?t=12631