Solved

How do I add in an onChange event to a dropDownList or Select List?

Posted on 2011-09-09
5
326 Views
Last Modified: 2012-05-12
What sample code can you give me that adds an onChange event to a form created
with PHP controls. There should be a way to add the onChange event and idealy call back to PHP or javascript.
At the HTML level I could use Javascript but only after updating the HTML generated by:
CHtml::dropDownList

I have code that comes close but there is no targeting of select controls it works on all select controls.
Here are some things I have tried.
I have tried writing Javascript code right out oreilly's Javascript book but when applied to
<select id="myCB1" > ...</SELECT>   they don't work
namely using
<script>v
var b=document.getEleimentById("myCB1")  
b.attachEvent("onChange,myfunc); or b.addEventListener("Change",myfunc)

See the code that kind of works that uses JQuery below, however, it does not solve my problem of  a targeted solution for just myCB1 select box or dropdown list
When I have many dropdownlists or as they become in HTML  SELECT controls:

 

 
!DOCTYPE html> 
<html> 
<head> 
  <style> 
 
  div { color:red; } 
  </style> 
  <script src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 
  <select name="sweets" > 
    <option>Chocolate</option> 
    <option selected="selected">Candy</option> 
 
    <option>Taffy</option> 
    <option>Caramel</option> 
    <option>Fudge</option> 
    <option>Cookie</option> 
 
  </select> 
  <div></div> 
<script> 
    $("select").change(function () { 
          var str = ""; 
          $("select option:selected").each(function () { 
                str += $(this).text() + " "; 
              }); 
          $("div").text(str); 
        }) 
        .change(); 
</script> 
 
</body> 
</html>

Open in new window

0
Comment
Question by:Robert Silver
  • 4
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36511046
use class, and id or a nam attribute
for example you set the name attribute of your select : sweets
you may use :

    $("select[name='sweets']").change(function () {
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36511101
0
 
LVL 2

Author Closing Comment

by:Robert Silver
ID: 36511601
You did not not finish your work there. But Your answer was right on target:
Here is what you should have done:
The code works best with completeness. Makes everything crystal clear
Same HTML but the script should be:
<script>
  $("select[name='sweets']").change(function () {
          var str = "";
        $("select[name='sweets'] option:selected").each(function () {
                str += $(this).text() + " ";
              });
          $("div").text(str);
        })
        .change();
</script>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36511628
my work? lol
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36511642
and you did not check the link provided, same as your final solution (with a tip)

so at the end : Thanks for the B grade!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
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)

932 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

18 Experts available now in Live!

Get 1:1 Help Now