Solved

Selected list item with javascript

Posted on 2007-11-14
9
2,751 Views
Last Modified: 2008-02-20
Hi,

this question is a continuation from the following question;

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22961965.html

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.

0
Comment
Question by:jset_expert
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 17

Accepted Solution

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

<html>
      <head>
            <title>Script Demo Gops&reg;</title>
            <style>
                  body, table,input, select,span{font-family:verdana;font-size:xx-small;}
            </style>
            <script language="javascript">
                  var c=0;
                  function changeOpt() {
                        var s=document.getElementById('sel');
                        c++;
                        if(c<s.options.length)s.selectedIndex=c;
                  }
            </script>
      </head>
      <body>
            <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>
            </select>
            <br>
            <input type="button" value="Change" onclick="changeOpt()">
      </body>
</html>
0
 
LVL 1

Author Comment

by:jset_expert
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.
0
 
LVL 17

Expert Comment

by:gops1
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.
0
 
LVL 1

Author Comment

by:jset_expert
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.

thanks
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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
setCookie('countrystate',document.frmCheckout.ddlCountry.selectedIndex+'|'+document.frmCheckout.ddlState.selectedIndex);

in your onChange of each select and add this script
<script language="javascript">

function init() {

  var countryIndex = document.frmCheckout.ddlCountry.selectedIndex; 

  if (countryIndex > 0) {

    getStates(document.frmCheckout.ddlCountry.options[document.frmCheckout.ddlCountry.selectedIndex].value);

  }

  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

    }

  }

}

</script>

Open in new window

0
 
LVL 1

Author Comment

by:jset_expert
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?
0
 
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;
to
   echo $js_droplist;
   echo "selbox.selectedIndex=".$selectedStateIndex; // assuming it did not change
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20943736
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now