Here’s the code for the form as it stands:
<form onsubmit="return false;" role="search" method="get" id="searchform" action="window.location.reload()" autocomplete="off">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" class="searchbar" >
</div>
</form>
Since I’m actually using a ‘live search’ plugin from wordpress (searches without navigating to another page), and it has a bug where deleting and re-entering the same text does not search again, I was wondering how I would get the page to reload if the user just pressed enter in the search box?
My second question is how to get the search bar to be highlighted or selected by default upon the page being loaded, just like Google? I’ve tried this:
<body onload"
$(function() {
$("input[name='s']").focus();
});
">
But it doesn’t seem to do anything. Any help on either of these problems would really be appreciated!
You’re not escaping the double quotes in the
onloadfunction. You can fix that by escaping the quotes with backslashes, but a better way to fix this is to put this code in a<script>tag (instead of inline):When using the
$()function like this, it acts as a shortcut for$(document).ready(), so there’s no need to attach it to the body’sonloadevent.Just to clarify, the other way to fix it would be to use escaped single quotes, like so:
But I highly recommend you put this in a
<script>tag. It’s better not to use inline JavaScript, and with jQuery there is no need to.