Solved

jQuery onChange

Posted on 2015-02-24
8
61 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 43

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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 43

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 43

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)

740 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