I am returning form elements into a form from ajax. I can count the number of elements returned, but I don’t know how to cycle through them. I need to be able to get the value of each element returned.
I am pretty sure this is a basic javascript thing that I just don’t know. The problem only looks more complicated with the Ajax.
My code looks like this:
// The view page
<html>
<head>
<script language="javascript">
function calculateAlphaTotals(){
var length = document.myForm["alpha[]"].length;
alert( length ); // correctly outputs 3
for( var i = 0; i < length; i++ ){
try{
alert( document.myForm["alpha[]"].value ); // HTML ObjectCollection
alert( document.myForm["alpha["+i+"]"].value ); // Object Required
} catch( error ) { }
}
}
</script>
</head>
<body>
<form name="myForm" id="myFormId" method="post">
<div id="ajaxOutputId"></div>
</form>
</body>
</html>
// The Ajax page:
<input name="alpha[]" onchange="calculateAlphaTotals()" />
<input name="alpha[]" onchange="calculateAlphaTotals()" />
<input name="alpha[]" onchange="calculateAlphaTotals()" />
try using
document.myForm["alpha[]"][i].valueinstead ofdocument.myForm["alpha["+i+"]"].valueso you function will be like this