biggieuk Posted June 4, 2008 Share Posted June 4, 2008 Hi, I have a web page with a dynamically populated dropdown box and a textarea. Currently when a value is selected the page reloads and the database data corresponding to the value of the dropdown is returned to the textarea, however the dropdown does not keep to the same value. On page reload i have appended a ?id=<id here>. The javascript is: <script language="javascript" type="text/javascript"> function load() { document.getElementById('lstGroup').selectedValue = <? echo $id; ?>; } </script> It works if i change selectedValue to selectedIndex but i want to set the dropdown box to the value in the $id variable. help with this would be very much appreciated. thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 4, 2008 Share Posted June 4, 2008 In my opinion, this should be handled server-side when generating the select list. If you really want to do it with JS, then either add the option index to the passed values so you canuse selectedIndex. Otherwise you need ot iterrate through every option to find the one that matches the value then set the selectedIndex that way. <script language="javascript" type="text/javascript"> function load() { selObj = document.getElementById('lstGroup'); for (i=0; i<selObj.options.length; i++) { if(selObj.options.text=='<?php echo $id; ?>') { selObj.selectedIndex = i; break; } } } </script> Quote Link to comment Share on other sites More sharing options...
biggieuk Posted June 5, 2008 Author Share Posted June 5, 2008 thanks for your reply, however i cant seem to get this working. Would using, if(selObj.options.text=='<?php echo $id; ?>') look for the dropdown text value to equal the id ? Whereas i am looking to set the dropdown to the same VALUE as 'id'. I tried changing .text to .value but the dropdown doesn't change on page reload? thanks again for your help. Quote Link to comment Share on other sites More sharing options...
haku Posted June 5, 2008 Share Posted June 5, 2008 You should do this server-side as mjdamato said. When you are outputting the option tags in your server-side script, if the option value is the same as the get value, then add selected="selected" to your option tag. So you would want something like this. For example your url is http://www.example.com/page.php?id=5 $value = 5; <option value="<?php echo $value; ?>"<?php if $value = $_GET['id'] { echo " selected =\"selected\""; ?>>option value</option> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 5, 2008 Share Posted June 5, 2008 Would using, if(selObj.options.text=='<?php echo $id; ?>') look for the dropdown text value to equal the id ? Whereas i am looking to set the dropdown to the same VALUE as 'id'. I tried changing .text to .value but the dropdown doesn't change on page reload? .text is the option Label and .value is the option value. Use whichever it is you are trying to accomplish. I tested that code before I posted, so I know it works. As long as the $id value is inthe array you are searching (text or value) it will work. Quote Link to comment Share on other sites More sharing options...
biggieuk Posted June 5, 2008 Author Share Posted June 5, 2008 Thanks to the both of you. You were right, Server side is a lot better method. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.