Solved

jQuery onChange

Posted on 2015-02-24
8
54 Views
Last Modified: 2015-03-07
I have a jQuery function that is an onChange function when any select values change.  Is there anyway to have the onChange function, but NOT have it fire when one specific select is changed?
0
Comment
Question by:rgranlund
  • 4
  • 3
8 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40628495
so you have something like
$('select').on('change', function(){  ... });

Open in new window


If you want to exclude some selects from this selector you need to either
- specify the selects one by one
- add code in the event handler to identify the select and skip the code
- mark the selects to be excluded or included with a custom class

I like the last option, and you can implement it like:
<select class="trackchange"></select>
$('select.trackchange').on('change', function(){  ... });

Open in new window

<select trackchange></select>
$('select[trackchange]').on('change', function(){  ... });

Open in new window

0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40629661
After you've bound the change event to all your <select> elements, just unbind it from the one you don't need it for:

$('select').change(function(){
    alert($(this).val());
});

$('select[name=someSelect]').unbind('change');

Open in new window

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40630094
@Chris idea is feasible if you have to attach to a lot of selects and only detach one or two, otherwise it's just becomes a mess of useless code.

In my opinion, marking the ones we want to control and attach only to those is a much better/cleaner solution.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40630115
@Alexandre - re-read the OPs question - he wants to bind to all Selects except one. That's exaclty what my code does. Not sure how that constitutes a mess of useless code.

Your code is feasible if you only want certain selects bound, but you have to unnecessarily edit your HTML and add extra classes to get it to work! If you need to change the behaviour, you'd have to edit your HTML!! Your other 2 suggestions aren't really feasible, given the amount of extra code you'd need.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40630136
Don't take it personal mate! :)
I even said that if it's really just one it's OK! :)

The mess will come if/when he actually needs to exclude more that that, having one line to bind, and 10 to unbind.

Cheers!
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40630158
Hey Alexandre. No worries - I never take it personally :)

My point was simple - I answered the question that was asked, and I stand by it.

It makes life a lot easier if you keep behavioural code and content separate. If you need to change the behaviour, then change the jQuery. If, as in your example, you need to suddenly not attach the event to 10 selects, then you'd have to go and edit the HTML and remove the classes on 10 elements. Now think about what happens if you have this mechanism on 10 pages - that's 100 changes you need to perform, as oppose to 1 one change - and jQuery can select more than one element at a time, so you could still do it with 1 line of code, and you haven't had to search through and mess with the semantics of your document.

As I said, your approach is feasible, but does require that the HTML structure itself is editied, just to change 'behaviour'.

Always good to have different options to acheive the same outcome :)
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40630225
Hi,
nice discussion. I just want to top this by adding another possiblity:
$('select:not("[name=xxx]")').change(function(){
    alert($(this).val());
});

Open in new window


Sample:
http://jsfiddle.net/vj9zebn6/

What about this? Using the selector to filter the selects?

Thanks
Rainer
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40630242
Ah! That's why I love being a developer! :)

@Chris: Good point on separating behavior and content.
@Rainer: It works but the 'not' pseudo selector is a bit heavy: Sizzle source code
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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)

747 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

10 Experts available now in Live!

Get 1:1 Help Now