1

How can I call search function? lot's of code written outside search function so I want to execute all code How can I do this please help me.

jQuery:

$(function(){
 function search(){
 alert("Hello World");
 }
})

HTML Code:

<select onchange="search();">
 <option value="1">Hello</option>
</select>
asked Apr 27, 2017 at 16:23
1
  • You've asked this same question three times now, and had the same answer each time Commented Apr 27, 2017 at 16:30

1 Answer 1

6

Any functions you call from onxyz-attribute-style event handlers must be global functions. That's one of the many reasons not to use them. Your search isn't global, it's nicely contained within your ready handler. Which is good, globals are a Bad ThingTM.

Hook up the function within your ready callback. Any selector for the select element will do; here's an example using an ID, but it doesn't have to be an ID:

<select id="the-select">
 <option value="1">Hello</option>
</select>

and

$(function(){
 $("#the-select").on("change", search);
 function search(){
 alert("Hello World");
 }
})

If you don't call search from any other code, you can combine that:

$(function(){
 $("#the-select").on("change", function search(){
 // Name here is optional --------------^--- but handy for debugging
 // if an error is thrown in the function
 alert("Hello World");
 });
})
answered Apr 27, 2017 at 16:26
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.