Solved

This should be simple - detect option:selected

Posted on 2013-06-05
5
168 Views
Last Modified: 2013-06-06
After the page loads, I need an event in Query that will tell me if the option value changes (selected="selected").  Even if what was selected is selected again...the event still needs to fire (possible?)

I want to show an alert(  )   showing what the drop down was changed to.

<select name="ctl08$ddlShortSaleParticipantApproval" onchange="javascript:setTimeout('__doPostBack(\'ctl08$ddlShortSaleParticipantApproval\',\'\')', 0)" id="ctl08_ddlShortSaleParticipantApproval" style="width:100px;">
	<option value="0">Unassigned</option>
	<option value="1167">Approved</option>
	<option selected="selected" value="1168">Denied</option>
	<option value="1384">Reopen</option>

</select>

Open in new window



My failed attempts:

  $('select[id*="ddlShortSaleParticipantApproval"] option:selected').change(function () {
            //$(this).css('background-color', 'green');
            alert('fire:  ' + $(this).text());
            // $('select option:selected').each(function () {
//            $('option:selected').each(function () {
//                // $(this).css('background-color', 'yellow');
//                if ($(this).text() == "Approved") {
//                    alert('found:  ' + $(this).text());
//                }
//                //        alert($(this).text());
//            });
        });

Open in new window




Now -- this code (below) works THE FIRST TIME, but subsequent choices do not fire the event after the first time:


Why isn't change(  )  firing the additional times?  Shouldn't it fire EACH time I change the selected option to something else?

 $('select[id*="ddlShortSaleParticipantApproval"]').change(function () {           
            alert('fire:  ' + $(this).text());
 });

Open in new window

0
Comment
Question by:knowlton
  • 3
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
use a fake dropdown box or change your logic
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
Fake drop down not an option.  I need to use the current custom control.

What do you mean by a logic change?


Could the alert(  ) be interfering?  Like causing a loss of focus?  (I'll look at that here in a minute)
0
 
LVL 5

Author Closing Comment

by:knowlton
Comment Utility
I needed to use live(  ) because the event was getting "unhooked":


 $('select[id*="ddlShortSaleParticipantApproval"]').live("change", function () {

        var hue = 'rgba(' + (Math.floor((256-199)*Math.random()) + 200) + ',' + (Math.floor((256-199)*Math.random()) + 200) + ',' + (Math.floor((256-199)*Math.random()) + 200) + ',.8)';
    
        $('div').css("background-color", hue);  //changes with each selection...
    
        });

Open in new window

0
 
LVL 42

Expert Comment

by:David S.
Comment Utility
Ah. That would do it.

It's best to avoid using live():
As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
Thanks...I'll correct that!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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

14 Experts available now in Live!

Get 1:1 Help Now