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 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to set Drop Down Menu Value

    Hi,

    How do I set a value for dropdown menu using Javascript?
    Can I still set a particular value if that particular value is not included as one of the option in the dropdown menu?

    Cheers

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Is this what you're looking for?

    document.theForm.theSelect.options[theIndex].value = theValue;

  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Almost, but that code is to set the value of a particular option in the drop down menu. What I want is to set a new option as the value of the drop down menu, and the value must is not one of the option of the drop down menu.

    I hope the code below helps me explain what I want.

    HTML
    Code:
    <form name="form1">
    <select name="cssFile" id="cssfile">
    <option value="file1">file1</option>
    <option value="file2">file2</option>
    <option value="file3">file3</option>
    </select>
    </form>

    Javascript
    Code:
    document.form1.cssFile.value = file4;
    cheers

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    var sel = document.form1.cssFile;
    sel.options[sel.options.length] = new Option(optionText, optionValue);

  • #5
    New Coder
    Join Date
    Apr 2005
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great Thanks.

    Another question.
    How do I remove a specific option from the drop down menu?

    Cheers

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Just set it to null.

    sel.options[theIndex] = null;

  • #7
    New Coder
    Join Date
    Apr 2005
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok. thanks.

    How do I check if a particular option of the dropdown menu is there?

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Once an option item is set to null, it is removed from the select and the options below it (if any) goes up. Say you have 3 items

    <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    </select>

    and option 1 is removed. Option 2 will now take the position of option 1, and option 3 takes the former position of option 2.

    <select>
    <option>2</option>
    <option>3</option>
    </select>


  •  

    Posting Permissions

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