0

I am trying to handle a change event for a dropdrown which looks like this:

 <div>
    <select id="serviceLine">
      <option selected="selected">--Select Option--</option>
      <option>Integration</option>
      <option>BPM</option>
      <option>GWT</option>
      <option>Other</option>
    </select>
 </div>

Now,I want to add a textarea when the user selects option "Others". The jQuery looks like this:

function otherHandler(){
       $(this).siblings("textarea").remove();
       if($(this).val()=="Other"){

              var textbox="<textarea rows='3'></textarea>";
              $(this).parent().append(textbox);
       }

}

$("#serviceLine").on("change",function(){otherHandler()});

This doesn't work, because in otherHandler() $(this) contains the reference of the entire window and not just the dropbox.

However if I change my jQuery to this, it works fine:-

function otherHandler(that){
       $(that).siblings("textarea").remove();
       if($(that).val()=="Other"){

              var textbox="<textarea id='slOther'rows='3'></textarea>";
              $(that).parent().append(textbox);
       }

}


$("#serviceLine").on("change",function(){otherHandler(this)});

My question is why didn't it work in the first case, why do we have to pass the reference explicitly? Am I missing something major here?

1
  • If you have otherHandler wrapped in an anonymous function for some reason, like there's other code before or after it, then you can obtain the proper this value in otherHandler() by using .call() or .apply() to invoke the function. otherHandler.call(this) Commented Feb 14, 2014 at 17:45

2 Answers 2

2

In your first case it didn't worked as this is for defined for the event handler.

$("#serviceLine").on("change",function(){
    // this is accessible here in event handler not in the otherHandler function call
     otherHandler();
});

You should have directly passed the reference of function

$("#serviceLine").on("change", otherHandler);

If you wish you can use .apply(this)

function otherHandler(){
       $(this).siblings("textarea").remove();
       if($(this).val()=="Other"){

              var textbox="<textarea rows='3'></textarea>";
              $(this).parent().append(textbox);
       }

}

$("#serviceLine").on("change",function(){
   otherHandler.apply(this);
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct solution. Not sure why it's getting downvoted. +1
1

Raed this keyword

$("#serviceLine").on("change",function(){
    //this --> is local to the function block here you cannot use it outside
});

$("#serviceLine").on("change",function(){otherHandler(this)});
                                                      //^

Here you pass the reference of this to the function so it works


Better use

$("#serviceLine").on("change", otherHandler);
                                //^ function name

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.