469,313 Members | 2,338 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,313 developers. It's quick & easy.

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
Feb 26 '08 #1
1 839
gits
5,390 Expert Mod 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. function select_opt_by_val(obj, val) {
  2.     var opts = obj.getElementsByTagName('option');
  3.  
  4.     for (var i = 0, n; n = opts[i]; i++) {
  5.         if (n.value == val) {
  6.            n.selected = true;
  7.            break;
  8.         }
  9.     }
  10. }
  11.  
  12. var s = document.getElementById('my_select');
  13.  
  14. select_opt_by_val(s, 'C');
  15.  
kind regards
Feb 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.