/    Sign up×
Community /Pin to ProfileBookmark

dropdown list onchange problem

[CODE]
<script type=”text/javascript”>
function ni(form) {
ni=parseFloat(form.ni.value);
form.ans4.value = (form.lista1.value == “one”)?0.62 :
(form.lista1.value == “two”)?0.8 :
(form.lista1.value == “three”) ?0.9 :

‘error’;
}

var ni;
var error;
if (form.lista1.value == “one”)
{
document.write(ni=0.62);
}
else if (form.lista1.value == “two”)
{
document.write(ni=0.8);
}
else if (form.lista1.value == “three”)
{
document.write(ni=0.9);
}

else
{

document.write(error);
}
</script>

<FORM name=”form2″>
<table width=”93″ border=”1″>
<tr>
<td colspan=”2″>formula</td>
</tr>
<tr>
<td height=”66″ colspan=”2″><div align=”center”>
<select name=”lista1″ size=”1″ id=”lista1″ onchange=”javascript: dropdownlist(this.options[this.selectedIndex].value);”>
<option value=”one”>one</option>
<option value=”two”>two</option>
<option value=”three”>three</option>
</select>
</div></td>
<tr>
<td>ni =</td>
<td><input type “number” value=”” name=”ans4″ size=”9″ /></td>
</tr>

</table>
</FORM>
[/CODE]

What am i doing wrong?

i want to display values in textbox on select…when user select one it should write 0.62 etc…
pls help

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Sep 18.2010 — Why are you redefining the function name of ni and using it as a variable inside and than you use it as a local in a few lines later. Very bad practice there. Use variable names that have meaning.

If you want the value of a select, use something like:

[CODE]var mySelect = document.formName.elementName;
var selectedOption = mySelect.options[mySelect.selectedIndex];
var txt = selectedOption.text;
var val = selectedOption.value;
[/CODE]
Copy linkTweet thisAlerts:
@FangSep 18.2010 — [CODE] <select name="lista1" size="1" id="lista1" onchange="this.form.ans4.value=this.value;">
<option value="0.62">one</option>
<option value="0.8">two</option>
<option value="0.9">three</option>
</select>
[/CODE]
×

Success!

Help @dimacar 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.18,
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,
)...