Selected list item with javascript

Posted on 2007-11-14
Last Modified: 2008-02-20

this question is a continuation from the following question;

How set the selected value from the state drop down with Javascript?

As you can see from the previous question, I build my drop down list with PHP and javascript. When the page is refreshed, then so is the value that the user selected.

Question by:jset_expert
  • 3
  • 2
  • 2
  • +1
LVL 17

Accepted Solution

gops1 earned 150 total points
ID: 20286934
Here is a sample code on how to change the values of a dropdown through javascript:

            <title>Script Demo Gops&reg;</title>
                  body, table,input, select,span{font-family:verdana;font-size:xx-small;}
            <script language="javascript">
                  var c=0;
                  function changeOpt() {
                        var s=document.getElementById('sel');
            <select id="sel">
                  <option value="">--Select--</option>
                  <option value="Opt1">Opt 1</option>
                  <option value="Opt2">Opt 2</option>
                  <option value="Opt3">Opt 3</option>
                  <option value="Opt4">Opt 4</option>
            <input type="button" value="Change" onclick="changeOpt()">

Author Comment

ID: 20286984
Hi can you relate that code back to my drop list, which is dynamically assigned using PHP and Javascript.

i can see that the '.selectedIndex' function will set the option, but not sure how to associate this back to my existing code.
LVL 17

Expert Comment

ID: 20287002
I am weak in PHP, but tell me one thing what value are you going to pass, is it the number, or the value that you are going to pass.
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.


Author Comment

ID: 20287092
i will pass a number, this will be the state unique id from the db, which is also the value for the option on the drop list.

LVL 75

Expert Comment

by:Michel Plungjan
ID: 20287465
I am assuming your have a country drop and a state drop

If the statedrop needs to match the country drop you could do this:

I also assume you have a standard cookie script - if not, I can give you one

You need to do a

in your onChange of each select and add this script
<script language="javascript">
function init() {
  var countryIndex = document.frmCheckout.ddlCountry.selectedIndex; 
  if (countryIndex > 0) {
  var sels = getCookie('countrystate');
  if (sels) {
     var countrystate = sels.split('|');
    if (countryState[0] == countryIndex) { // did we have the same country as previous
      document.frmCheckout.ddlState.selectedIndex=countryState[1]; // set the state to the same state

Open in new window


Author Comment

ID: 20293987
I am not using cookies at all at the moment.

All the data is stored with variables on the current page.

I can grab the state variable selected, do I need to use cookies?
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 150 total points
ID: 20296859
If you have the index in the php, change
   echo $js_droplist;
   echo $js_droplist;
   echo "selbox.selectedIndex=".$selectedStateIndex; // assuming it did not change

Expert Comment

ID: 20943736
Forced accept.

Community Support Moderator

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can i gnupg encrypt a PDF document? 12 33
where is team.js? 3 26
JavaScript let vs var 5 27
How can I make a javascript to show drop down 4 20
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question