Web Analytics Made Easy -
StatCounter Cant figure this out - CodingForum

Announcement

Collapse
No announcement yet.

Cant figure this out

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cant figure this out

    So what im trying to do is to get a value from two dropdowns. something doesnt get right. Anyone know whats wrong with my code?

    This is my html code:
    Code:
    <form>
        <select name="courselevel" id="courselevel" style="width:120px">
            <option>Courses level</option>
            <option value="1">level 5</option>
            <option value="2">level 4</option>
            <option value="3">level 3</option>
    		<option value="3">level 2</option>
    		<option value="3">level 1</option>
        </select>
        <br/>
       <select name="tolevel" id="tolevel" style="width:120px">
            <option>To level</option>
            <option value="1">level 4</option>
            <option value="2">level 3</option>
    		<option value="3">level 2</option>
    		<option value="4">level 1</option>
        </select>
        <br/>
    	<input name="button" type="button" value="Submit" onclick="return validate()" />   
        <input name="reset" type="reset" value="Reset" />
    </form>
    this is my js:


    Code:
    var courselevel = document.getElementById('courselevel'),
        tolevel = document.getElementById('tolevel');
    	
    	window.onload = function(){
    courselevel = document.getElementById('courselevel'),
    tolevel = document.getElementById('tolevel');
    
    courselevel.addEventListener('change', function(e) {
        switch (level.selectedIndex) {
            case 0:
                var courselevelList1   = ["Select level"];
                var courselevellist2    = ["Select level2"];
                fillList(courselevel, courselevelList1);
                fillList(tolevel, courselevelList2);
                break;
        
            case 1:
        
                var courselevelList   = ["Select level", "5", "4", "3", "2", "1"];
                var courselevelList2    = ["Select level2", "4", "3", "2", "1" ];
    			fillList(level, courselevelList1);
                fillList(tolevel, courselevelList2);
                break;
    
        }
    });
        
    };
    
    
    
    
    function fillList(el, items) {
         el.innerHTML = '';
        for (var i = 0; i < items.length; i++) {
            var option = document.createElement('option');
            option.value = items[i];
            option.text = items[i];
            el.add(option);
        }
    }
    
    
    window.validate = function() {
        var courselevelvalue = parseFloat(courselevel.options[courselevel.selectedIndex].value),
            tolevelvalue = parseInt(tolevel.options[tolevel.selectedIndex].value),
            totaltolevelvalue = courselevelvalue + tolevelvalue;
        
        total.innerHTML = totaltolevelvalue;    
        return totaltolevelvalue;

  • #2
    Originally posted by AdasR View Post

    So what im trying to do is to get a value from two dropdowns. something doesnt get right. Anyone know whats wrong with my code?
    Well obviously you are trying to do more
    than just get the drop down values,
    anyways here is some code that
    gets the drop down values.

    Code:
    <script>
        window.onload = function(){
           var courselevel = document.getElementById('courselevel'),
           tolevel = document.getElementById('tolevel');
           document.forms[0].addEventListener('submit', 
              function(evt){
                 evt.preventDefault();
                 alert(courselevel.options[courselevel.selectedIndex].value);
                 alert(tolevel.options[tolevel.selectedIndex].value);             
              }
           );
       }
    </script>
    <form>   
        <select name="courselevel" id="courselevel" style="width:120px">
            <option value="0">Courses level</option>
            <option value="1">level 1</option>
            <option value="2">level 2</option>
            <option value="3">level 3</option>
    	<option value="3">level 4</option>
    	<option value="3">level 5</option>
        </select>
        <br/>
       <select name="tolevel" id="tolevel" style="width:120px">
            <option value="0">To level</option>
            <option value="1">level 1</option>
            <option value="2">level 2</option>
    	<option value="3">level 3</option>
    	<option value="4">level 4</option>
        </select>
        <br/> 
        <input name="reset" type="reset" value="Reset" >
        <button>submit</button>
    </form>
    That probably is not much
    help to you, maybe give
    more explanation of what
    you are trying to do and
    what you are having problems with.
    we are all immigrants who face immanent deportation back to heaven

    Comment

    Working...
    X