/    Sign up×
Community /Pin to ProfileBookmark

multiple dynamic fields with the same name..

I have a function that goes through a set of three text fields

[code=html]<input name=”quantity” type=”text” size=”5″ value=”1″ onblur=”UpdateQty(this)”/>
<input name=”quantity” type=”text” size=”5″ value=”1″ onblur=”UpdateQty(this)”/>
<input name=”quantity” type=”text” size=”5″ value=”1″ onblur=”UpdateQty(this)”/>[/code]

yes they all share the name quantity and this is because I will have PHP render more if needed dynamically.

I also have a price of 39.00 for the products and only 1 text box which should display the total.

Here is my javascript function…

[code=html]
function UpdateQty() {

//this will count how many times the PHP while loop has gone through..currently
//the session is set a 3 so it will take out 3 products with a quantity box and
//price of 39.00
var counter = document.forms[0].counter.value;

var i=0;
while (e = document.forms[0].elements[counter++]) {
if (e.type == ‘text’)
{
//if I do this then it does the total for the array value of quantity
//qty = document.forms[0].quantity[0].value;
qty = document.forms[0].quantity[e].value;
totalcost = qty * 45.00;
document.forms[0].elements.total.value = “$”+parseFloat(totalcost).toFixed(2);
}
}
}
[/code]

I want this function to go through all the quantity fields and then multiply to the price and finally to the total. When I do set the following line

[I] qty = document.forms[0].quantity[e].value;[/I]

it give me an error

[I]Error: document.forms[0].quantity[e] has no properties[/I]

but if i hardcode only one field of the quantity such as:

qty = document.forms[0].quantity[0].value;

It does multiply by the quantity of that field and display the total..anyone know how I can go through all the quantity fields and mutiply all those against the product price and display total? thanks any suggestions or help is appreciated.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JJul 12.2005 — Does this help in any way?


[code=php]function UpdateQty() {

//this will count how many times the PHP while loop has gone through..currently
//the session is set a 3 so it will take out 3 products with a quantity box and
//price of 39.00
var counter = document.forms[0].elements["quantity"].length

var i=0;
var totalcost=0

while (i != counter) {

if (document.forms[0].elements[i].type == 'text') {
qty = document.forms[0].quantity[i].value;
totalcost += qty * 45.00
document.forms[0].total.value = "$"+parseFloat(totalcost).toFixed(2);
i++
}

}

}[/code]
Copy linkTweet thisAlerts:
@vicpal25authorJul 12.2005 — it gave me an infinte looop..

[I]function UpdateQty() {



//this will count how many times the PHP while loop has gone through..currently

//the session is set a 3 so it will take out 3 products with a quantity box and

//price of 39.00

var counter = document.forms[0].elements["quantity"].length



var i=0;
var totalcost=0;

while (i != counter) {

if (document.forms[0].elements[i].type == 'text') {

//if I do this then it does the total for the array value of quantity

//qty = document.forms[0].quantity[0].value;

qty = document.forms[0].quantity[i].value;

totalcost += qty * 45.00

document.forms[0].total.value = "$"+parseFloat(totalcost).toFixed(2);

i++

}

}

}



</script>[/I]
Copy linkTweet thisAlerts:
@Mr_JJul 12.2005 — Try my test file

[code=php]
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script>

function UpdateQty() {

//this will count how many times the PHP while loop has gone through..currently
//the session is set a 3 so it will take out 3 products with a quantity box and
//price of 39.00
var counter = document.forms[0].elements["quantity"].length

var i=0;
var totalcost=0

while (i != counter) {

if (document.forms[0].elements[i].type == 'text') {
qty = document.forms[0].quantity[i].value;
totalcost += qty * 45.00
document.forms[0].total.value = "$"+parseFloat(totalcost).toFixed(2);
i++
}

}

document.getElementById("div1").innerHTML="Total quatity elements = "+counter+"<br>Total iterations = "+i


}

</script>
</HEAD>
<BODY>

<div id="div1">Total quantity elements =<br>Total iterations =</div>

<form name="standardForm">
<input name="quantity" type="text" size="5" value="1" onblur="UpdateQty(this)"/>
<input name="quantity" type="text" size="5" value="1" onblur="UpdateQty(this)"/>
<input name="quantity" type="text" size="5" value="1" onblur="UpdateQty(this)"/>

<input name="total" type="text" size="5" value=""/>
</form>

</BODY>
</HTML>[/code]
Copy linkTweet thisAlerts:
@vicpal25authorJul 12.2005 — yeah that works.hummm...i need to rework my file..
×

Success!

Help @vicpal25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...