?
Solved

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

Posted on 2011-09-09
5
Medium Priority
?
342 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 83

Accepted Solution

by:
leakim971 earned 1500 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 83

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 83

Expert Comment

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

749 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