Dropdown list onchange problem


<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>

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

^ +1 definitely more relevant and elegant for the problem in question.

Why jQuery? learning it would get him to write right code at a faster pace in case he doesn’t have time to become good at js first.
Mostly from a productivity point of view.

I don’t see the function ni(form) ever being called up.

You’re using dropdownlist function in “onchange” event, you should paste its code too.
document.write usually a strings -

document.write(error);

error is undefined, so is ni=…, put them in quotes instead to make sense.

But then you could shorten your code as I see redundant statements -

<script type="text/javascript">
function ni(form) {
		var ni=parseFloat(form.ni.value);
		var ans4 =  (form.lista1.value == "one")?0.62 : 
                           (form.lista1.value == "two")?0.8 :
				           (form.lista1.value == "three") ?0.9 : 
						   'error';
                form.ans4.value = ans4;
                document.write ((ans4=='error')? ans4: 'ni=' + ans4);
}
		
</script>

Beware of using document.write under XHTML.

I would recommend you to start learning jQuery to write these codes in an easy and failsafe manner.

  1. you haven’t shown where you actually call the function ni(form) in your code.

  2. you can’t use document.write() after the page has finished loading because document.write writes to a new document after the original page had been loaded. You should be using the DOM methods to display function outputs on the original page.

Why jQuery for something so simple?

<select name="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>

You will have a better understanding of jQuery if you first learn some basic JavaScript.