www.webdeveloper.com
Results 1 to 3 of 3

Thread: dropdown list onchange problem

  1. #1
    Join Date
    Sep 2010
    Posts
    12

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

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    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;

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    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>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles