473,322 Members | 1,526 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,322 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 3188
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"...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.