PDA

View Full Version : simple calculation



Redsim
09-30-2006, 03:59 AM
hye experts..
im a very newbie baby with javascript and need some help.

Its about a match calculation
my problem is so simple, there is 2 drop down list
what im trying to do is, how can i add the selected value of dropdownA and dropdownB
and then display the value of it


sorry all..im to naive :o

jscheuer1
09-30-2006, 06:50 AM
Technically speaking, there is no selected value of a drop down other than that assigned to its selected object (its selected option's value attribute). One can get the text object of the selected option as well. In either case this is a string variable, not a number variable. If it happens to be a number it can be converted to a number variable and then math may be performed on it.

For anyone to understand what you are trying to do though would require at least an example of a typical situation in which you wish to carry out these operations. Where you want the result displayed would be helpful too. An idea of what this would be used for, even better.

Twey
09-30-2006, 11:46 AM
<form action="">
<select onchange="document.getElementById('mOutput').firstChild.nodeValue = parseFloat(this.value) + parseFloat(this.form.elements['two'].value);" name="one">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select onchange="document.getElementById('mOutput').firstChild.nodeValue = parseFloat(this.value) + parseFloat(this.form.elements['one'].value);" name="two">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
<p id="mOutput"> </p>

jscheuer1
09-30-2006, 03:18 PM
Twey, a good basic overview but, it makes certain assumptions about the form that may or may not not apply to what Redsim has in mind and I'm reasonably sure that it will not work in IE. In IE you need the select's this.options[this.selectedIndex].value where you have its this.value. And similarly for the value of the other select when used in the onchange event of one select.

Twey
09-30-2006, 03:48 PM
I'm reasonably sure that it will not work in IE.It will.
Twey, a good basic overview but, it makes certain assumptions about the form that may or may not not apply to what Redsim has in mindI wasn't aware that Redsim had a form already built. If so, he certainly didn't mention it.

jscheuer1
09-30-2006, 04:07 PM
From the IE javascript error dialogue:


'document.getElementById(...).firstChild' is nul or not an object

I was wrong about the other thing though, that part works.

Opera doesn't work as expected either.

Putting this in the head:


<script type="text/javascript">
onload=function(){
var tot=document.createTextNode('');
document.getElementById('mOutput').appendChild(tot);
}
</script>

fixes IE but not Opera. Opera gives no error though, just doesn't work.

jscheuer1
09-30-2006, 04:18 PM
After further testing, this appears to be the only reliable x-browser method:


<form action="">
<select onchange="document.getElementById('mOutput').innerHTML = parseFloat(this.value) + parseFloat(this.form.elements['two'].value);" name="one">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select onchange="document.getElementById('mOutput').innerHTML = parseFloat(this.value) + parseFloat(this.form.elements['one'].value);" name="two">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
<p id="mOutput"></p>

And the problem with Opera was that it saw the appended text node not as the first child. However, Opera would work if the empty space was removed from the p element, this threw of FF though. With enough object testing it could probably be made to work with the DOM but, I think that this is a case where innerHTML is appropriate for use with a form, it isn't being used in the form so, it won't overwrite the form's values.

Redsim
09-30-2006, 04:31 PM
wow..it works fine...
thanks to prof jscheuer1 and Twey..
actually i've made some typo error in my 1st post..
the exact word is 'math' not 'match'...
sorry to jscheuer1 for making you confusing :)

Redsim
09-30-2006, 05:22 PM
actually my real situation are like this..

im trying to combine java struts with javascript.
i have a table generated with <display:column>
every row of the generated table contain a dropdown

how can i place arrays for the table rows in the javascript to
count every dropdown values selected in every row.

is it possible to combine it? if so please give me some guide as a kickstart