Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  16] [ 6]  / answers: 1 / hits: 30400  / 11 Years ago, thu, august 1, 2013, 12:00:00

i am using select2.



Usage : if a value is COUNTRY, i want to add a onchange event to the select2, else i want to remove the onchange event as shown below :



var reportLevel = getReportLevel();

if (reportLevel != COUNTRY) {
$(#selected_countries).on(change, function(e) {
prepareGlidModel(e);
});
} else {
$(#selected_countries).on(change, function(e) {
});
}


Issue : Not being able to remove the onchange event, even if the else block is called.
Events are called based upon the reportLevel value selected in a dropdown.


More From » jquery

 Answers
126

try following:



var reportLevel = getReportLevel(),
// by default unbind/off change event
$select = $(#selected_countries).off(change);

// and if country then bind it
if (reportLevel == COUNTRY) {
$select.on(change, function(e) {
alert(you selected : + $(this).val());
});
}


Working example here: http://jsfiddle.net/JB89B/1/


[#76589] Wednesday, July 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kenyonmasonc

Total Points: 44
Total Questions: 117
Total Answers: 116

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;