1

I know similar questions have been asked, but I couldn't find any answer. There is a JavaScript function:

function init(url) {
    $('#id_search').keyup(function() {
        performsearch(url, e);
    });
}

I am trying to pass event object and the url parameter to the performsearch function:

function performsearch(url, e){
        e.preventDefault();
        console.log('perf->' + url);
    }

But e.preventDefault() gives an error:

e is not defined

What is wrong in this function call?

2
  • well e is never defined.... Commented Jun 24, 2017 at 12:54
  • .keyup(function(e){ rather .on("input", function(e){ ?! The oninput event will trigger also on copy paste etc events... Commented Jun 24, 2017 at 12:58

2 Answers 2

4

You need to get the event from the keyup's callback function.

function init(url) {
    // Get the event parameter of the callback function
    $('#id_search').keyup(function (e) {
        performsearch(url, e);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this with closure. Your problem may be solved using other solutions but you might encounter problems later on that you might need this technique.

     $('#id_search').keyup(function(url) {
         return function (evt) {
             evt.preventDefault();
             console.log('perf->' + url); 
        };

     })();

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.