I have this code :
$(function (){
$('#searchText2').keyup(function(event){
if (event.which >= 38 && event.which <= 40)
return;
var val = $(this).val();
jQTubeUtil.suggest(val, function(response){
var html = '';
for(s in response.suggestions){
var sug = response.suggestions[s];
html += '<li class="uli2"><a href="#">'+sug+'</a></li>';
}
if (response.suggestions.length)
$('#autocomplete').html(html).show();
else
$('#autocomplete').hide();
});
});
});
I would like to run this code when an input is onblur or onfocus with HTML: <input name="q" type="text" onblur="**WHAT DO I PUT HER TO RUN CODE**">
I did give the function a name but that did not work.
You needn’t attach the event handler in the HTML.
You’ll want to keep your JS (jQuery) code seperate from your HTML and to do this you can use the event handling mechanisms available to you in jQuery.
I see @Tatu just posted a code sample as I’m writing this so I wont add the same thing here.
The important part here is to know that you never need to use the HTML event attachment mechanism. It’s outdated and rarely has a valid place these days.