I have a WordPress site that needs search. Its not the greatest search engine on the world so I'm trying to make it a little better using radio buttons like so:
Fiddle: http://jsfiddle.net/usMg6/3/
<form method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<div id="search-inputs">
<input type="text" value="" name="s" id="s" />
<input class="search-all-words" type="radio" name="search-radio" value="AllWords">All Words <input class="search-exact-phrase" type="radio" name="search-radio" value="ExactPhrase">Exact Phrase
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
As you can see, I've added a group of 2 radio buttons. One for exact phrase and one for all words.
As you can also see I've managed to get the section that adds quote marks around the words in the search box if the 'exact phrase' radio button is clicked. However I have a an issue in that I don't know enough about jQuery to make the rest of it work.
So what I need is:
1) To ensure if the 'exact phrase' radio button is clicked multiple times, it only adds one set of quotes around the phrase
2) To make the radio button that controls 'all words' add AND between all the spaces between the words and also if the 'exact phrase' radio button is clicked after the 'all words' button had been clicked and 'AND' had been added it removes the word 'AND' and adds quotes around the phrase.
Hope that all makes sense! Any help much appreciated.
I have updated your fiddle http://jsfiddle.net/eY9RH/.
$( ".search-exact-phrase" ).click(function() {
var searchInputStr = $("input#s").val().replace(/['"]/g,'');
searchInputStr = searchInputStr.split(' AND ').join(' ')
$("input#s").val('\"' + searchInputStr + '\"');
});
$( ".search-all-words" ).click(function() {;
var searchInputStrAll = $("input#s").val().replace(/['"]/g,'');
searchInputStrAll = searchInputStrAll.replace(/\s+$/, '');
$("input#s").val(searchInputStrAll.split(' ').join(' AND '));
});
hope this will help you.