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>
-
You've asked this same question three times now, and had the same answer each timeRory McCrossan– Rory McCrossan2017年04月27日 16:30:21 +00:00Commented Apr 27, 2017 at 16:30
1 Answer 1
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");
});
})