1

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.

5
  • why not add a parameter to your function updateView like this updateView(para). Send true to this from where you want to happen this change for others don't send Commented Jul 18, 2014 at 10:29
  • Hi, this is a way to solve it. But I have a lot of function is using this updateView, if I add in a parameter, I need to do many modification. Thus, I prefer not to do this. Commented Jul 18, 2014 at 10:32
  • 1
    no you dont need just dont send from other end Commented Jul 18, 2014 at 10:33
  • As you're already using jQuery, why attach a handler like this onchange="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. Commented Jul 18, 2014 at 10:33
  • it will be undefined for others Commented Jul 18, 2014 at 10:34

3 Answers 3

4

You can add more than one handler to same event, this means you can leave the existing function and write a new one and then add this function to dropdown so that other mechanisms triggering updateView() would be unaffected.

$('[name="accountDateRange"]').on('change',function(){
alert('this is specific to this dropdown only');
})
Sign up to request clarification or add additional context in comments.

2 Comments

@LIUFA, have edit on my question. Kindly help again.
$('[name="accountDateRange"]').on('change',function(){ alert('this is specific to this dropdown only'); }) goes into the page in separate <script></script>, and not into the function updateView()
1

I'd do that in just jQuery without adding onchange to select element

js

$(document).ready(function(){
  $('#mySelect').bind('change', function(e){
    updateView(e.currentTarget.id)
  })
})

updateView = function (selectId) {
  if(selectId !== 'mySelect'){
    return;
  }
  alert('called from mySelect')
}

working plunker http://plnkr.co/edit/EmSowz6osgse2Ou8g6FC?p=preview

1 Comment

Your idea is better. +1
0

onclick is not well for dropdown list just in the onchange function you need to use your function. And decide to use a parameter like this:

function updateView(bool) {
   if(bool){
    //do your stuff
   }
}

Another way is to add your new function in new onchange as mentioned by LIUFA but please make sure to use stopPropagation or return false to make it work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.