how to get the value of document.getElementById

Discussion in 'JavaScript' started by macaela, Jul 12, 2012.

  1. #1
    Hi I have a form with two select drop down i've gave each their own different ID but now i am trying to use the document.getElementById on both but its only picking up second one how can I get so that depending on the select iD user picks it would be like if document.getElementById("location") alert location else if document.getElementById("color") alert color.

    I have tried but its just call the second document.getElementById("color")

    Heres' the form
    
    <form action="<?php bloginfo('url'); ?>" method="get" class="x">
    <select id="location" >
     <option value="america">Great Site</option>
     <option value="england">Better Site</option>
    </select>
    
    <select id="color" >
     <option value="blue">Great Site</option>
     <option value="red">Better Site</option>
    </select>
    </form>
    HTML:
    here the function I have tried but doesnt work it skips the first if statment how can i get to select the if statement depending on the select ID the user picks
    function()
    	{
    	 var selectBox = document.getElementById("location");
    	 var selectBox1 = document.getElementById("color");
       	 var selectValue = selectBox.options[selectBox.selectedIndex].value;
    	
    
       		if (selectBox == "location")
    			{
    			alert("location");
    			}
    			else
    		 if (selectBox1 == "color");
    			{
    				alert("color");
    			}
    		
    	}}}(jQuery,window,undefined));
    Code (markup):
     
    macaela, Jul 12, 2012 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    There are a couple of instances in your code where the 'if' statements aren't using the correct variable. They test the object and not the object's 'value' property.
     
    rainborick, Jul 12, 2012 IP
  3. macaela

    macaela Active Member

    Messages:
    181
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Hi i have tried this way the alert on the first function runs which show the function is working but then it doesnt send the value holder into the second function? help me please


    <script type="text/javascript">
    function category1()
    {
    var carname="Volvo";
    alert (carname)
    document.getElementById("holder").innerHTML=carname;
    }
    </script>
    Code (markup):
    but when get to the if statment functions it doesnt run

    function()
    	{
    	 var selectBox = document.getElementById(holder);
       	 var selectValue = selectBox.options[selectBox.selectedIndex].value;
    	
       // location.href = 'http://www.gopherdeals.co.uk/?dl='+ selectValue +'&s=+&dc=';
       		if (document.getElementById("addidas"))
    			{
    			alert("sports");
    			}
    			else
    		 if (document.getElementById("Volvo"));
    			{
    				alert("cars");
    			}
    Code (markup):
     
    macaela, Jul 12, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Your logic flow makes no sense at all -- you have both selects,they both have values, why the else? OF COURSE its only returning one of them.

    For the life of me I can't even figure out what you are even trying to do in either code example, neither of which makes the least bit of sense whatsoever. Can you explain what it is you are actually trying to do, because your code is unclear/nonsensical... You get the two selects, you then pull the value of just one of them, then you try to do a string compare against two [object] results, meaning NEITHER if statement should actually run...
     
    deathshadow, Jul 14, 2012 IP