?
Solved

jQuery onChange

Posted on 2015-02-24
8
Medium Priority
?
72 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
LVL 44

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
 
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 44

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

719 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