Issue
I'm coding a custom search bar in HTML and jQuery.
The code I have so far:
$(document).ready(function() {
$('.submit_on_enter').keydown(function(event) {
// enter has keyCode = 13, change it if you want to use another button
if (event.keyCode == 13) {
this.form.submit();
return false;
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="outercontain">
<div class="innercontain" align="center">
<form action="https://google.com/" method="get">
<input autocomplete="off" class="submit_on_enter" type="text" name="q" placeholder="Search Art">
</form>
</div>
</div>
What I'm looking to do is limit searches to a specific site purely in html/in the URL itself. On the actual Google webpage this is easy - you could just enter "site:website.com"
and it would be done. But in this setup I have, name="q"
is already appended to any searches. If I get rid of the name="q"
section, the search doesn't work properly (it just goes to google's homepage) and if I tamper with the url, then it just searches for whatever I add. DuckDuckGo is acceptable too.
Solution
You need to put the site:website.com
as part of the value of q
not the key.
$(document).ready(function() {
$('.submit_on_enter').keydown(function(event) {
// enter has keyCode = 13, change it if you want to use another button
if (event.keyCode == 13) {
this.form.q.value = "site:website.com " + this.form.q.value
this.form.submit();
return false;
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="outercontain">
<div class="innercontain" align="center">
<form action="https://google.com/" method="get">
<input autocomplete="off" class="submit_on_enter" type="text" name="q" placeholder="Search Art">
</form>
</div>
</div>
Side note: the snippet won't actually redirect because of stackoverflow's security on the iframe.
Answered By - powerc9000
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.