Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Drop Down List Boxes

    Hi,
    I'm new to java script and I'm having a problem with writing a variable to store a selected item of a drop down list box. Do I have to write a condition statement like I have had to for the radio buttons I have used, if so how would I word it, for my radio buttons I used;
    if (document.forms[0].sex.checked)
    gender = "male";
    else
    gender = "female";
    could I use something simlar to this.... if I did this could I just declare the variable simply as var colour as colour is the name of my drop down list box.

    Many thanks.

    Yorkie107

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    not 100% sure what you mean... but you could do

    var result = document.forms[0].listname.value;

    or

    var result = document.forms[0].listname.selectedIndex;

    that what you mean?

  • #3
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    using var colour = document.forms[0].listname.selectindex; gives me undefined any idea how I colud define it.

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <body>
    <form>
    <select name="listname"></select>
    </form>
    </body>

  • #5
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No still returning undefined see if anyone can spot what I have done wrong

    My variable is: var colour = document.forms[0].colour.selectedindex;

    My code to return the value in my table is:
    document.write("<tr><td><strong>Colour Selected:</strong></td>");
    document.write("<td>" + colour +"</td></tr>");

    My code for my drop down list box is:
    <select name="colour">
    <option>blond</option>
    <option>brown</option>
    <option>green</option>
    </select>

    Many thanks

    Yorkie107

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's: selectedIndex
    Not: selectedindex

    Note the uppercase I.....

  • #7
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this gives me a numerical value ie if Blond is selected it give a value of 1 Brown gives a value of 2 could I use an else if statement to change the value of 1 back to Blond ect.

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try something like:
    document.forms[0].colour.options[document.forms[0].colour.selectedIndex].text

  • #9
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there a way for me to set another variable on the selection of a drop down item?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •