Solved

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

Posted on 2011-09-09
5
327 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses four methods for overlaying images in a container on a web page
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.

776 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