• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 97
  • Last Modified:

jQuery onChange

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
rgranlund
Asked:
rgranlund
  • 4
  • 3
1 Solution
 
Alexandre SimõesManager / Technology SpecialistCommented:
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
 
Chris StanyonCommented:
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
 
Alexandre SimõesManager / Technology SpecialistCommented:
@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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Chris StanyonCommented:
@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
 
Alexandre SimõesManager / Technology SpecialistCommented:
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
 
Chris StanyonCommented:
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
 
Rainer JeschorCommented:
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
 
Alexandre SimõesManager / Technology SpecialistCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now