/    Sign up×
Community /Pin to ProfileBookmark

IE won’t display this script

I created a form with a dropdown list as seen below:

[CODE]<form action=”#” name=”family” id=”family”>
Price range for a family of
<select name=”number” id=”number” style=”width:40px; margin-left:2px;” onchange=”giveEstimate()”>
<option value=””></option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
</select>: <span id=”showrange”></span>
</form>[/CODE]

Here’s my corresponding JS:

[CODE]<script type=”text/javascript”>
<!–
function giveEstimate() {
document.getElementById(“showrange”).innerHTML = “”;
var n = document.getElementById(‘number’);
var numdiv = document.createElement(‘div’);
numdiv.id = “results”;
if (n.value == ‘2’) {
numdiv.innerHTML = “$990 – $1,030”;
}
if (n.value == ‘3’) {
numdiv.innerHTML = “$1,190 – $1,330”;
}
if (n.value == ‘4’) {
numdiv.innerHTML = “$1,380 – $1,630”;
}
if (n.value == ‘5’) {
numdiv.innerHTML = “$2,170 – $2,360”;
}
if (n.value == ‘6’) {
numdiv.innerHTML = “$2,370 – $2,660”;
}
if (n.value == ‘7’) {
numdiv.innerHTML = “$2,560 – $2,960”;
}
if (n.value == ‘8’) {
numdiv.innerHTML = “$2,760 – $3,260”;
}
var oldHTML = document.getElementById(“showrange”).innerHTML;
document.getElementById(“showrange”).innerHTML = oldHTML + numdiv.innerHTML;
}
</script>[/CODE]

The script should display the text associated with whatever number is chosen. This works in Firefox, but IE just sits there and twiddles its thumbs. I’m pretty new to doing JS so I’m sure there’s some small something that I’m missing to get it working. Any advice on a better way to code this is appreciated.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Arty_EffemJan 05.2008 — [CODE]
<option value=""></option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>[/code]
[/QUOTE]
To read values from option tags, they must first be assigned values.
Copy linkTweet thisAlerts:
@scragarJan 05.2008 — you also shouldn't use n.value

use n.options[n.selectIndex].value you are getting the value of an option, not the box.
Copy linkTweet thisAlerts:
@bradymcauthorJan 05.2008 — Arty,

That worked. I made the assumption that if the value wasn't specified it defaulted to whatever the option is. I thought that had worked for me in the past.

Scragar,

I replaced 'n.value' with 'n.options[n.selectIndex].value' and everything stopped working? It does make more sense to use 'n.options[n.selectIndex].value'. However, I don't know enough about JS to know why it caused it to stop working.

Thank you both for your help!
Copy linkTweet thisAlerts:
@scragarJan 05.2008 — sorry, it's select[b]ed[/b]Index not selectIndex. my mistake entirely.

on a side note I should point out that you could completely do away with testing the value of select boxes if your only going to assign them numbers anyway.

&lt;script type="text/javascript"&gt;
&lt;!--
function giveEstimate() {
document.getElementById("showrange").innerHTML = "";
var n = document.getElementById('number').selectedIndex;
var numdiv = document.createElement('div');
numdiv.id = "results";
switch(n){
case 1:
numdiv.innerHTML = "$990 - $1,030";
break;
case 2:
numdiv.innerHTML = "$1,190 - $1,330";
break;
case 3:
numdiv.innerHTML = "$1,380 - $1,630";
break;
case 4:
numdiv.innerHTML = "$2,170 - $2,360";
break;
case 5:
numdiv.innerHTML = "$2,370 - $2,660";
break;
case 6:
numdiv.innerHTML = "$2,560 - $2,960";
break;
case 7:
numdiv.innerHTML = "$2,760 - $3,260";
break;
};
var oldHTML = document.getElementById("showrange").innerHTML;
document.getElementById("showrange").innerHTML = oldHTML + numdiv.innerHTML;
}
&lt;/script&gt;
but I wouldn't recomend using anything you don't understand.
Copy linkTweet thisAlerts:
@Arty_EffemJan 05.2008 — Arty,

That worked. I made the assumption that if the value wasn't specified it defaulted to whatever the option is. I thought that had worked for me in the past.
[/QUOTE]
Why not just set the values to the "$" strings, and pass the value to the div directly?
×

Success!

Help @bradymc 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.16,
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,
)...