Jump to content

[SOLVED] Retain dropdown value


biggieuk

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/108712-solved-retain-dropdown-value/
Share on other sites

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>

 

 

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.

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>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.