By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

I thought this would be easy - HELP

P: n/a
EJ
var theSel = document.forms['price'].elements['cpu'];
var theVal = theSel.options[theSel.selectedIndex].value;
var theSli = theVal.slice(1,3);
this doen't seem to work

var cpus = document.forms['price'].elements['cpu'];
var cpusx = cpus.options[cpus.selectedIndex].value;
var cpusxd = cpusx.slice(8,10);

when I use the event handler

onchange="document.forms['price'].value=cost(cpusxd,shpsxd)"

I get the error message: "cpudsx undefined

Mar 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"EJ" <jo********@hotmail.comwrote in news:1174703621.570832.232670
@o5g2000hsb.googlegroups.com:
>var theSel = document.forms['price'].elements['cpu'];
var theVal = theSel.options[theSel.selectedIndex].value;
var theSli = theVal.slice(1,3);

this doen't seem to work

var cpus = document.forms['price'].elements['cpu'];
var cpusx = cpus.options[cpus.selectedIndex].value;
var cpusxd = cpusx.slice(8,10);

when I use the event handler

onchange="document.forms['price'].value=cost(cpusxd,shpsxd)"

I get the error message: "cpudsx undefined

Check your spelling: ( cpusxd vs. cpudsx ) One of the limitations of
variable names you can't pronounce.

Also, your onchange code lacks any mention of which element in the form
you want to change the value.
Mar 24 '07 #2

P: n/a
"EJ" <jo********@hotmail.comwrote in news:1174703621.570832.232670
@o5g2000hsb.googlegroups.com:
>var theSel = document.forms['price'].elements['cpu'];
var theVal = theSel.options[theSel.selectedIndex].value;
var theSli = theVal.slice(1,3);

this doen't seem to work

var cpus = document.forms['price'].elements['cpu'];
var cpusx = cpus.options[cpus.selectedIndex].value;
var cpusxd = cpusx.slice(8,10);

when I use the event handler

onchange="document.forms['price'].value=cost(cpusxd,shpsxd)"

I get the error message: "cpudsx undefined

Put everything in a function. The variables cpus, cpusx, cpusxd must be
evaluated after the onchange event happens.

function myFunc = {
var cpus = document.forms['price'].elements['cpu'];
var cpusx = cpus.options[cpus.selectedIndex].value;
var cpusxd = cpusx.slice(8,10);
document.forms['price'].elements['something'].value=cost(cpusxd,shpsxd)
}

onchange = "myFunc();"
Mar 24 '07 #3

P: n/a
EJ
On Mar 24, 1:10 am, Jim Land <rrrrfffftttt(no)@(spam)hotmail.com>
wrote:
"EJ" <jordane...@hotmail.comwrote in news:1174703621.570832.232670
@o5g2000hsb.googlegroups.com:
Put everything in a function. The variables cpus, cpusx, cpusxd must be
evaluated after the onchange event happens.
thanks, but why?

also you wrote:
Are you trying to insert the sum into the hidden element?
document.forms['price'].elements['amount'],value = cpusxd + shpsxd;
this does not produce a sum, or rather the following function does not
produce a sum

function total()
{
var cpus = document.forms['price'].elements['cpu'];
var cpusx = cpus.options[cpus.selectedIndex].value;
var cpusxd = cpusx.slice(6);
var shps = document.forms['price'].elements['shp'];
var shpsx = shps.options[shps.selectedIndex].value;
var shpsxd = shpsx.slice(7);
document.forms['price'].elements['amount'].value = cpusxd + shpsxd;
}

please note:

<select name="cpu" id="cpu" onchange="total();">
<option selected value="AMDx - 2500">

<select name="shp" id="shp" onchange="total();">
<option selected value="3Day - 100">

....
onchange="total();"returns 2500100

Mar 25 '07 #4

P: n/a
"EJ" <jo********@hotmail.comwrote in news:1174794390.053055.114290
@y66g2000hsf.googlegroups.com:
document.forms['price'].elements['amount'].value = cpusxd + shpsxd;
Sorry. This should work:

document.forms['price'].elements['amount'].value = +cpusxd + shpsxd;

The + in front of cpusxd converts from string to number.
Mar 25 '07 #5

P: n/a
In comp.lang.javascript message <Xns98FDF13CA7CB7rrrrffffttttnospamho@21
6.168.3.44>, Sun, 25 Mar 2007 04:42:52, Jim Land
<rr**********@hotmail.composted:
>"EJ" <jo********@hotmail.comwrote in news:1174794390.053055.114290
@y66g2000hsf.googlegroups.com:
> document.forms['price'].elements['amount'].value = cpusxd + shpsxd;

Sorry. This should work:

document.forms['price'].elements['amount'].value = +cpusxd + shpsxd;

The + in front of cpusxd converts from string to number.
You need to test not only your code but also your "corrections".

That should be ... = +cpusxd + +shpsxd; since + between Number and
String converts the Number to String and concatenates.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 26 '07 #6

P: n/a
Dr J R Stockton <jr*@merlyn.demon.co.ukwrote in news:Uh74ap4FA
$B*****@invalid.uk.co.demon.merlyn.invalid:
That should be ... = +cpusxd + +shpsxd; since + between Number and
String converts the Number to String and concatenates.
I stand corrected.
Mar 27 '07 #7

P: n/a
EJ
thanks, you have all been a great help

Mar 28 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.