Good day,
I have a drop down list where have a onchange function call updateView(). jsp code is as follow:
<s:select name="accountDateRange" onchange="updateView();" id="function" >
However, there is not only this drop down list using this function. Some other function may call updateView() to proceed.
I have some code need to run when user do onchange on this drop down list, but I do not want this code to be run when other function call updateView().
Following is part of code of updateView():
function updateView() {
// some other code here
// here is the code that I only want to run during onchange
$("#fromDate").val("");
// some other code here
}
I have try to put in a onclick function in the dropdown, in this onclick function, I will set a variable to true.
So that I can using this variable to do checking before run the $("#fromDate").val("");, like:
if (onchangeOnly = true){
$("#fromDate").val("");
}
However, I found that the onchange will run first compare with onclick.
Any way or idea to doing this? kindly advise.
EDIT :
function updateView() {
// some other code here
if ($("#function").val() == "TODAY") {
// some other code here..
}
else if ($("#function").val() == "DATERANGE") {
// here is the code that I only want to run during onchange
$('[name="accountDateRange"]').on('change',function(){
alert('this is specific to this dropdown only');
});
}
// some other code here
}
I found that, even my $("#function").val() is not equal to "DATERANGE", it will also print out the alert('this is specific to this dropdown only'). Kindly point out what is my mistake.
updateViewlike thisupdateView(para). Sendtrueto this from where you want to happen this change for others don't sendupdateView, if I add in a parameter, I need to do many modification. Thus, I prefer not to do this.jQuery, why attach a handler like thisonchange="updateView();", if you could just do it like posted in the answer below (by @LIUFA)? That way you'll know exactly which element fired up the event.undefinedfor others