Topic: changing selected index in dropdown (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=31165" title="Pages that link to Topic: changing selected index in dropdown (Page 1 of 1)" rel="nofollow" >Topic: changing selected index in dropdown <span class="small">(Page 1 of 1)</span>\

 
Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 07-27-2009 17:53

Hi,

I passing a variable to a web page that has a drop down. I want to use javascript to change the dropdown's selected index based conditionally on the variable that is passed. I don't know how to do this properly and am looking for some code snippet suggestions.

Thanks,

Boudga


I solved this pretty easily with jquery

(Edited by Boudga on 07-28-2009 01:01)

yossiadmon
Obsessive-Compulsive (I) Inmate

From: Israel
Insane since: Sep 2009

posted posted 09-07-2009 18:12
code:
<%
    String p = (request.getParameter("p")==null)?"":request.getParameter("p");
%>
<html>
      <script type="text/javascript">
          function init(){
              var s=document.getElementById("select");
              for(var i=0;i<s.options.length;i++){
                  if(s.options[i].value=="<%=p%>"){
                      s.options[i].selected=true;
                      break;
                  }
              }
          }
      </script>
    <body onload="init()">
        <select id="select">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
    </body>
</html>

DavidJCobb
Nervous Wreck (II) Inmate

From: United States
Insane since: Mar 2009

posted posted 09-07-2009 19:29

Couldn't you just set the SELECT's selectedIndex?

----------------------

yossiadmon
Obsessive-Compulsive (I) Inmate

From: Israel
Insane since: Sep 2009

posted posted 09-08-2009 13:09

What if your values are not 1,2,3 but unkown strings, then you can't assume what option contains the value and you need to iterate them all...

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 09-08-2009 18:26

If it's happening on page load, I would handle this server side personally...

yossiadmon
Obsessive-Compulsive (I) Inmate

From: Israel
Insane since: Sep 2009

posted posted 09-10-2009 09:35

I assume I would handle it server side as well but then it will be much harder to answer Boudga (depends on the language the server side is written)

coach
Nervous Wreck (II) Inmate

From:
Insane since: May 2011

posted posted 05-31-2011 11:04
Edit TP: spam removed


Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu