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 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    99
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question dropdown value from javascript.

    I have a value receiving from database and saved it to a Javascript variable. I would like to pass this as an option to dropdown. How to achieve this.

    I tried below code that didn't work. ( I want the name "Country" in dropdown option).

    Code:
    window.onload = hello;
    function hello()
    {
    	var dd = document.getElementById("dropdownID");
    	   dd.options[dd.selectedIndex].value = "Country";
    	   
     }
    
    below is dropdown. 
    
    <select id="dropdownID" onchange="hello()">
    <option value="one"> </option>
    </select>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    This works perfectly ....

    But maybe you didn't want to change the value attribute but rather the visibile text?
    Code:
    dd.options[dd.selectedIndex].innerHTML = "Country";

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    or
    Code:
    dd.options[dd.selectedIndex].text = "Country";

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Do you want to change the text of the only option in the select? Or did you want to add an option, giving the select two options?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    99
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I want to add only one option. devnull69 and xelawho answered it correctly. thanks you all. However, it is only taking the text and not the value (i am speaking about <option value="country">). Tried the below code but not effect. Let me know if I am making anything wrong. This is because, if I submit again, it is taking blank values.


    Code:
    dd.options[dd.selectedIndex].text = "Country";
    Last edited by naveendk.55; 09-25-2012 at 03:58 PM.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    they're two separate attributes, so you would have to do something like this:
    Code:
    dd.options[dd.selectedIndex].text = "Country";
    dd.options[dd.selectedIndex].value = "Country";


  •  

    Posting Permissions

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