/    Sign up×
Community /Pin to ProfileBookmark

js function does not work correctly in IE

i found a function on this site, that i have modified to suit my requirements and is now working correctly in firefox, but in internet explorer it slowly returns 0.00

[CODE]/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Jay Rumsey | http://www.nova.edu/~rumsey/ */

function UpdateCost() {
var sum = 0;
var num = 0;
var gn, elem, qn, qty;

for (i=1; i<6; i++) {
gn = ‘w’+i;
qn = ‘q’+i;
elem = document.getElementById(gn);
qty = document.getElementById(qn);
num = qty.value
if (num == ‘Quantity’) {num = 0; }
document.getElementById(‘Phone bus’).value = num;
if (elem.checked == true) { sum += (Number(elem.value) * Number(num)); }
num = 0;
}
document.getElementById(‘Phone ah’).value = sum.toFixed(2);[/CODE]

the idea is to get the value, of the quantity from the select list, for the checked items

i am really new to js and this is just the first part of the script, which has taken a while to get working
**oh and the target ids are not the end result, just much easier to see when testing, probably bad form design

any suggestions to why it does not work in internet explorer, and how to fix
thx pete

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 14.2009 — can we see the HTML code as well? I presume you have some options in a select, but those options have no value, only text
Copy linkTweet thisAlerts:
@westconn1authorMay 14.2009 — I presume you have some options in a select, but those options have no value, only text [/QUOTE]
[I]num = Number(qty.value)

if (isNaN(num)) {num = 0; }[/I]


well that was part of the problem, i now get the value of 0 returned by num

it also appears that the function only runs once, further onchange events do not work

here is a partial html code, please note i did not write this, only modify the form elements for script to work, the page is about a mile long
[code=html] <td width="302" height="25"><span class="style82">
<label>
<input name="Genesis 05" id="w1" value="75" type="checkbox" onchange="UpdateCost()">
</label>
2005 Genesis Syrah</span></td>
<td width="116" height="25"><span class="style82">$75.00 per bottle</span></td>
<td width="80" height="25"><label>
<select name="quantity" class="checkboxes" id="q1" onchange="UpdateCost()">
<option>Quantity</option>
<option>6</option>
<option>12</option>
<option>18</option>
<option>24</option>
<option>30</option>
<option>36</option>
<option>42</option>
<option>48</option>
</select>
</label></td>
[/code]

there are currently 6 checkboxes and selects in a table

also for whatever reason this is working fine in firefox

does it not see a [B]select [/B]in getelementbyid ??

i think i've got this figured now, selects don't have value property in IE, i will find what property to use tomorrow

i will be back with next question when i get this working
Copy linkTweet thisAlerts:
@KorMay 14.2009 — That is exactly what I thought. Give your options [I]value[/I] attributes.

Despite other browsers, IE does not take automatically the [I]text[/I] of an option as it's [I]value[/I] in case the [I]value[/I] is missing. This affects not only javascript but the submit process as well (IE will send no value to the CGI onsubmit). This might be considered a bug, even I think IE is right. [I]value [/I]and [I]text[/I] are different properties.

<i>
</i>&lt;select name="quantity" class="checkboxes" id="q1" onchange="UpdateCost()"&gt;
&lt;option value=""&gt;Quantity&lt;/option&gt;
&lt;option [COLOR="Blue"]value="6"[/COLOR]&gt;6&lt;/option&gt;
&lt;option [COLOR="Blue"]value="12"[/COLOR]&gt;12&lt;/option&gt;
&lt;option [COLOR="Blue"]value="18"[/COLOR]&gt;18&lt;/option&gt;
&lt;option [COLOR="Blue"]value="24"[/COLOR]&gt;24&lt;/option&gt;
&lt;option [COLOR="Blue"]value="30"[/COLOR]&gt;30&lt;/option&gt;
&lt;option [COLOR="Blue"]value="36"[/COLOR]&gt;36&lt;/option&gt;
&lt;option [COLOR="Blue"]value="42"[/COLOR]&gt;42&lt;/option&gt;
&lt;option [COLOR="Blue"]value="48"[/COLOR]&gt;48&lt;/option&gt;


Give always values to the option elements.
Copy linkTweet thisAlerts:
@westconn1authorMay 14.2009 — thanks kor

i found that i could loop through the children to get selected, but this is [B]much [/B]better
×

Success!

Help @westconn1 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 5.8,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...