Onchange select

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    Onchange select

    Hi,

    I have 2 lists in a form one for a type of property and the other for price type. What i want to do is have a function so if i select property "Flats" then the price type menu will alter to Per Week.

    This is what i have so far but don't know how to go after this?

    Code:
       function doChange() {
    
          var sel = document.forms[0].property_type.selectedIndex;  
    
          if (document.forms[0].property_type[sel].value = "Flat") {
           
          }
    
       }
    HTML
    Code:
    <form>
    <select name="price_type" id="price_type" style="width:120px;">
         <option value="P/M">Let (Per Month)</option>
         <option value="P/W">Let (Per Week)</option>
         <option value="N/A">Buy</option>
        </select>
    
        <select name="property_type" id="property_type" style="width:120px;">
          <option value="">Type</option>
            <option value="Detached">Detached</option>
           <option value="Semi Detached">Semi Detached</option>
            <option value="Terraced">Terraced</option>
            <option value="Flat">Flat</option>
            <option value="Apartment">Apartment</option>
            <option value="Bungalow">Bungalow</option>
            <option value="Dormer Bungalow">Dormer Bungalow</option>
            <option value="Maisonette">Maisonette</option>
        </select>
    </form>
    Cheers,
    Adam
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Code:
       function doChange() {
    
    // you can use [I]sel[/I] as reference to the select element
    // would be more clear while reading code
          var sel = document.forms[0].property_type.selectedIndex;  
    
    // big mistake here, = is the assignment operator, use ==
          if (document.forms[0].property_type[sel].value[U] = [/U]"Flat") {
           
          }
    
       }
    some ideas I came up with
    Code:
       function doChange() 
        {
    // good names are half the code …
          var props = document.forms[0].property_type;  
    // do the same with the other select
          var price = document.forms[0].price_type;  
    
    // if you write incidentally = here, you’ll get an error
    // besides that the code is quite explanatory now
          if ("Flat" == props[props.selectedIndex].value) 
          {
               price.selectedIndex = 1;
          }
       }

    Comment

    • adamjblakey
      New Member
      • Jan 2008
      • 133

      #3
      Thank you very much for your reply, it works a treat :)
      Last edited by Dormilich; Aug 20 '09, 11:09 AM. Reason: Please use [code] tags when posting code

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        some additional improvements:
        Code:
        function doChange() 
        {
        	if ("Flat" == this[this.selectedIndex].value) 
        	{
        		document.forms[0].price_type.selectedIndex = 1;
        	}
        }
        document.forms[0].property_type.onclick = doChange;

        Comment

        Working...