...

View Full Version : noob needs help!



luv2pdl
05-02-2008, 07:28 PM
I konw this isnt right - but I don't know how to write an else statement.
I'm trying to use a radio button - to populate another field.
The code doesnt work because regardless of which button I select - the price displayed is $40 - which is the LAST item!

html code:
<tr><td>Item:</td><td>
<input type="radio" name="item" value="Vest" onClick="addPrice()" /> Vest<br />
<input type="radio" name="item" value="Shorts" onClick="addPrice()" /> Shorts<br />
<input type="radio" name="item" value="LSJersey" /> L/S Jersey<br />
<input type="radio" name="item" value="SSJersey" /> S/S Jersey<br />
</td>
</tr>


The javascript to populate the $ field:

function addPrice(){
if(webform.item.value="Vest")
{
webform.amount.value="95";
document.getElementById("amount").focus();
}
}

function addPrice(){
if(webform.item.value="Shorts")
{
webform.amount.value="50";
document.getElementById("amount");
}
}
function addPrice(){
if(webform.item.value="LSJersey")
{
webform.amount.value="60";
document.getElementById("amount");
}
}
function addPrice(){
if(webform.item.value="SSJersey")
{
webform.amount.value="40";
document.getElementById("amount");
}
}
function addPrice(){
if(webform.item.value="SSJersey")
{
webform.amount.value="40";
document.getElementById("amount");
}
}

Philip M
05-02-2008, 07:43 PM
Several major problems.

webform not defined.
Five functions all named addPrice. :eek::eek:

You should look at the way to access the values of radio buttons:-


<input type="radio" name="item" value="95" onClick="addPrice()" /> Vest<br />
<input type="radio" name="item" value="60" onClick="addPrice()" /> Shorts<br />
<input type="radio" name="item" value="50" /> L/S Jersey<br />
<input type="radio" name="item" value="40" /> S/S Jersey<br />

<script type = "text/javascript">
function addPrice() {
for (var i = 0; i < document.webform.item.length; i++) {
if (document.webform.item[i]. checked) {
amount = document.webform.item[i].value;
}
}
</script>


Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose.

luv2pdl
05-02-2008, 08:11 PM
I apologize about the title. I read the rules, but apparently missed that line...

The code makes sense to me about defining the values in the item field. However - the $ field is not populating at all now.

I moved the script code within my form (I had it in the <head> before).
IE gives me an error telling me that an object is expected at the line in my form.

I have now changed my code to read:
<tr><td>Item:</td><td>
<input type="radio" name="item" value="95" onClick="addPrice()" /> Vest<br />
<input type="radio" name="item" value="50" onClick="addPrice()" /> Shorts<br />
<input type="radio" name="item" value="60" onClick="addPrice()"/> L/S Jersey<br />
<input type="radio" name="item" value="40" onClick="addPrice()"/> S/S Jersey<br />
</td>
<script type = "text/javascript">
function addPrice(){
<!--alert ("hi");-->
for (var i = 0; i < document.webform.item.length; i++) {
if (document.webform.item[i]. checked) {
amount = document.webform.item[i].value;
}
}
</script>
</tr>
<tr>
<td>Amount</td><td><input name="amount" id="amount" size="7" /></td>
</tr>

rangana
05-03-2008, 03:07 AM
You haven't closed the script yet:


<script type = "text/javascript">
function addPrice(){
<!--alert ("hi");-->
for (var i = 0; i < document.webform.item.length; i++) {
if (document.webform.item[i]. checked) {
amount = document.webform.item[i].value;
}
}
}
</script>


Maybe you haven't created a form naming webform, This would work:


<form name="webform">
<table>
<tr><td>Item:</td><td>
<input type="radio" name="item" value="95" onClick="addPrice()" /> Vest<br />
<input type="radio" name="item" value="50" onClick="addPrice()" /> Shorts<br />
<input type="radio" name="item" value="60" onClick="addPrice()"/> L/S Jersey<br />
<input type="radio" name="item" value="40" onClick="addPrice()"/> S/S Jersey<br />
</td>
<script type = "text/javascript">
function addPrice(){
<!--alert ("hi");-->
for (var i = 0; i < document.webform.item.length; i++) {
if (document.webform.item[i]. checked) {
amount = document.webform.item[i].value;
alert(amount);
}
}
}
</script>
</tr>
<tr>
<td>Amount</td><td><input name="amount" id="amount" size="7" /></td>
</tr>
</table>
</form>

abduraooft
05-03-2008, 09:32 AM
and BTW, I request to read forum rules regarding subject of a thread, http://www.codingforums.com/postguide.htm

Philip M
05-03-2008, 09:34 AM
and BTW, I request to read forum rules regarding subject of a thread, http://www.codingforums.com/postguide.htm

That point has already been made, and the OP has apologised.

abduraooft
05-03-2008, 10:28 AM
That point has already been made, and the OP has apologised.
Oh.. I haven't seen that apology, but I think he could still edit his original post to change the title, rather than just apologising :)

Philip M
05-03-2008, 11:16 AM
Oh.. I haven't seen that apology, but I think he could still edit his original post to change the title, rather than just apologising :)

Stop hitting the guy!
He is a new coder and perhaps has not realised that he can edit the title.

abduraooft
05-03-2008, 11:34 AM
Sorry Philip. I'm not hitting anyone. I hadn't really found your notification about the title and the OP's response... (my badness). So, when I realised the fact, I just pointed out the edit feature. Hope luv2pdl has got his solution. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum