This is a migrated thread and some comments may be shown as answers.

Cancel Dropdown list change event

5 Answers 970 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Arun
Top achievements
Rank 1
Arun asked on 24 Jul 2012, 02:42 PM
Hi how do i cancel the dropdown change event?
i am doing a user conformation on dropdown change event. if the user confirm no then the dropdown selected value should go back to its old value. i tried return false but didnt work. 

$("#myDropdown").data("kendoDropDownList");
.bind("change", function (e) {
if(!confirm('warning'))
return false;
}
thanks for your help Arun

5 Answers, 1 is accepted

Sort by
0
Arun
Top achievements
Rank 1
answered on 27 Jul 2012, 06:43 PM
seems we cannot cancel the change event.  To achieve the desired functionality i had to bind to 'select' event and then do 
   e.preventDefault();
 	 
         
('#myDropdown').bind("select", function (e) {
           var data = this.dataItem(e.item.index());
            var current = this.value();  // this will have the old selected value
            var selectedType = data.Code; //this will have the new selected value
}

please feel to correct me if i am wrong :-) thanks, Arun
0
Elias
Top achievements
Rank 1
answered on 15 Nov 2012, 08:32 PM
You can't cancel the onChange event, but you can cancel the onSelect event. Call the preventDefault() function and it will cancel the event.

function onSelect(e) {
    e.preventDefault();
}

-- or --
('#myDropdown').bind("select", function (e) {
    e.preventDefault();
}
0
Tayger
Top achievements
Rank 1
Iron
answered on 21 Mar 2016, 10:18 AM

Thank you, works well also on my side... In case of having an optionLabel in dropownlist you have to add +1 to the index, else its always shifted by 1:

var data = this.dataItem(e.item.index()+1);

That shouldn't be since the optionLabel is no more an element of DOM and dropdownlist. 

0
Graham
Top achievements
Rank 1
Iron
answered on 10 Aug 2017, 02:28 PM
A workaround to not being able to cancel the event: put a conditional statement in the event function to prevent it from running its code if a certain condition applies. Set that condition in the code to "cancel" the event.
0
Edward
Top achievements
Rank 1
answered on 09 Mar 2019, 01:11 AM

Following works for me, with optionlabel set:

    let prevValue = this.text(); //this.selectedIndex
    let newValue = e.dataItem.Value; // e.dataItem.Id

Tags
DropDownList
Asked by
Arun
Top achievements
Rank 1
Answers by
Arun
Top achievements
Rank 1
Elias
Top achievements
Rank 1
Tayger
Top achievements
Rank 1
Iron
Graham
Top achievements
Rank 1
Iron
Edward
Top achievements
Rank 1
Share this question
or