Solved

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

Posted on 2011-09-09
5
324 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
Comment Utility
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
Comment Utility
0
 
LVL 2

Author Closing Comment

by:Robert Silver
Comment Utility
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
Comment Utility
my work? lol
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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

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.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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.
The viewer will learn how to dynamically set the form action using jQuery.
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)

762 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

13 Experts available now in Live!

Get 1:1 Help Now