I have the following jQuery that runs as a user types in an input box.
var $rows = $('#table tbody tr');
$('#search').keyup(function() {
var val = '^(?=.*\b' + $.trim($(this).val()).split(/s+/).join('\b)(?=.*\b') + ').*$',
reg = RegExp(val, 'i'),
text;
$rows.show().filter(function() {
text = $(this).text().replace(/s+/g, ' ');
return !reg.test(text);
}).hide();
});
How do I get the same jQuery to run if there’s already text in the input box on page load?
3
Answers
You can use
ready
function and check if textbox has text in it.Just move all the logic into its own named function:
You should move the logic into its own function but you can just trigger the keyup event if you don’t want to refactor what you have.