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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Learning DOM and getting all the elementsByTag

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
Here is the problem, I am learning DOM and am trying a few demo pages to learn I have created a page that holds 2 text boxes. I want to loop through the text boxes, grab the value and then display them some how. I dont care because I am just playing around. Can someone help me out. Thanks,
-T


Code:
<html>
<head>
	<title>This is a test</title>
	<script language="javascript">
	function doSomting(idName)
	{
		var obj = document.getElementById(idName).value;
		alert(obj);
	}
	function clearBox(idName)
	{
		document.getElementById(idName).value = "";
		
	}
	function infoMe()
	{
		var top = document.getElementById('topForm');
		var list = top.getElementsByTagName('text');
		alert(list.value);
	}
	</script>
	
</head>
<body>
<div id="topForm">
	<input type="text" id="firstOne" value="I'm Here">
	<input type="button" value="Clear" name="Clear" onclick="clearBox('firstOne')">
	<br />
	<input type="text" id="secondOne" value="">
	<input type="button" value="Clear" name="Clear" onclick="clearBox('secondOne')">
	<br />
</div>
<input type="button" value="Enter" name="Enter" onclick="doSomting('firstOne')">

<input type="button"  value="WHAT" onclick="infoMe()">

</body>
</html>

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
try changing the function to this:
Code:
    function infoMe()
    {
        var str = "";
        var list = top.getElementsByTagName('input');
        for (i = 0; i < list.length; i++) {
           if (list[i].type == "text") {
              str += list[i].value + "\n";
           }
        }
        alert(str);
    }

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
uncle_rico_thumb.jpg
 
You can use .getElementByTagName(), here is an example for your situation.


Since I see both your textboxes are within the div id="topForm", start with that.

Make an array of all the input fields within topForm.
Loop and get out the textboxes.

Code:
function infoMe() {
   var inputArray = document.getElementById("topForm").getElementsByTagName("input");
   var inputArrayLength = inputArray.length;
   for (a = 0; a < inputArrayLength; a++) {
      //filter out the textboxes here
      if (inputArray[a].type == "text") {
         alert(inputArray[a].value);
      }
   }
}

This code will alert the value of each textbox one at a time until they have both been alerted.


[monkey][snake] <.
 
Rock on people thanks.

I will take a look.

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top