Making the option as selected based on the specfic value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sivaani
    New Member
    • Jan 2008
    • 2

    Making the option as selected based on the specfic value

    hai friends

    i am doing my updating page their i have one select field i am getting the input from the user and saved the select field value alone in the db . now based on the select value from the db the select field option should be displayed in the page .that means the option displayed on the page should change based on the db value . how can i change based on the db value as selected in the select field?.

    thanks in advance

    sivaani.j
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    assuming you have a select like the following:

    [HTML]<select id="my_select" >
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    </select>
    [/HTML]
    you could use a function like this:

    [CODE=javascript]function select_opt_by_v al(obj, val) {
    var opts = obj.getElements ByTagName('opti on');

    for (var i = 0, n; n = opts[i]; i++) {
    if (n.value == val) {
    n.selected = true;
    break;
    }
    }
    }

    var s = document.getEle mentById('my_se lect');

    select_opt_by_v al(s, 'C');
    [/CODE]
    kind regards

    Comment

    Working...