473,238 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

Onchange select

133 100+
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2.    function doChange() {
  3.  
  4.       var sel = document.forms[0].property_type.selectedIndex;  
  5.  
  6.       if (document.forms[0].property_type[sel].value = "Flat") {
  7.  
  8.       }
  9.  
  10.    }
  11.  
HTML
Expand|Select|Wrap|Line Numbers
  1. <form>
  2. <select name="price_type" id="price_type" style="width:120px;">
  3.      <option value="P/M">Let (Per Month)</option>
  4.      <option value="P/W">Let (Per Week)</option>
  5.      <option value="N/A">Buy</option>
  6.     </select>
  7.  
  8.     <select name="property_type" id="property_type" style="width:120px;">
  9.       <option value="">Type</option>
  10.         <option value="Detached">Detached</option>
  11.        <option value="Semi Detached">Semi Detached</option>
  12.         <option value="Terraced">Terraced</option>
  13.         <option value="Flat">Flat</option>
  14.         <option value="Apartment">Apartment</option>
  15.         <option value="Bungalow">Bungalow</option>
  16.         <option value="Dormer Bungalow">Dormer Bungalow</option>
  17.         <option value="Maisonette">Maisonette</option>
  18.     </select>
  19. </form>
  20.  
Cheers,
Adam
Aug 20 '09 #1
3 3183
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.    function doChange() {
  2.  
  3. // you can use sel as reference to the select element
  4. // would be more clear while reading code
  5.       var sel = document.forms[0].property_type.selectedIndex;  
  6.  
  7. // big mistake here, = is the assignment operator, use ==
  8.       if (document.forms[0].property_type[sel].value = "Flat") {
  9.  
  10.       }
  11.  
  12.    }
some ideas I came up with
Expand|Select|Wrap|Line Numbers
  1.    function doChange() 
  2.     {
  3. // good names are half the code …
  4.       var props = document.forms[0].property_type;  
  5. // do the same with the other select
  6.       var price = document.forms[0].price_type;  
  7.  
  8. // if you write incidentally = here, you’ll get an error
  9. // besides that the code is quite explanatory now
  10.       if ("Flat" == props[props.selectedIndex].value) 
  11.       {
  12.            price.selectedIndex = 1;
  13.       }
  14.    }
Aug 20 '09 #2
adamjblakey
133 100+
Thank you very much for your reply, it works a treat :)
Aug 20 '09 #3
Dormilich
8,658 Expert Mod 8TB
some additional improvements:
Expand|Select|Wrap|Line Numbers
  1. function doChange() 
  2. {
  3.     if ("Flat" == this[this.selectedIndex].value) 
  4.     {
  5.         document.forms[0].price_type.selectedIndex = 1;
  6.     }
  7. }
  8. document.forms[0].property_type.onclick = doChange;
Aug 20 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
4
by: Bart van Deenen | last post by:
Hi all I have a script where I dynamically create multiple inputs and selects from a script. The inputs and selects must have an associated onchange handler. I have the script working fine on...
2
by: DonD | last post by:
I have a form that allows a user to upload a file. When they select a file (onChange), I call a JS function that analyzes the filename for specific keywords and then populates a multiple select box...
8
by: Yeah | last post by:
I wish to use a drop box where each Option will not take the user to a web page - but a certain location on the same page the drop box exists. For example, Option 1 would take the user to "Chapter...
3
by: b_naick | last post by:
I realize that the onChange event for a drop down can be trapped as follows: <select name="myDropDown" onChange="somefunc"> Is it possible to trap the onChange event outside of the select...
2
by: donald | last post by:
I have a function called populate which populate a select box. I need it to run when a different select box value is chnage. So I need the event onChange. But i need to pass it two var to. how...
1
by: Jan Doggen | last post by:
Hello all, I have a SELECT like this (the 'alert()s are temporary): <FORM method="POST" action="/scripts/runisa.dll?OVB2.132964:PGSPLITVACAFMELDEN:1095144287.9159" id="hulpform"...
21
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
14
by: The Natural Philosopher | last post by:
This is a nasty one and I can't see my way out of it. I have a bunch of select statements in a form, and each select statement has an onchange="do_something(this)" in it, and this works...
15
by: colyn7 | last post by:
I really can't see what's wrong in my code... the submit() onChange doesn't work.. I've tried.. <select name="ddlTestCenter" id="ddlTestCenter" style="width:180px"...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.