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
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>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.