0

i'm making a wordpress theme with a slide out search box. I would like to hide/toggle the search box by clicking away. Here is my javascript

jQuery(document).ready(function($){
    $("#flip").click(function(){
        $("#searchbox").toggle("slide", { direction: "right" }, 500);
    });

        $("#flip2").click(function(){
        $("#searchbox").toggle("slide", { direction: "right" }, 500);
    });

});


jQuery('body').on('click', function(e){
{
        $("#searchbox").toggle("slide", { direction: "right" }, 500);
});
jQuery('#searchbox').click(function(e)
{
   e.stopPropagation();
});

I get an unexpected token error when trying this. Thanks for your help

ScreamQueen
  • 206
  • 1
  • 4
  • 13

3 Answers3

1

this code should work-

jQuery(function() {

        jQuery('#flip').click(function() {
        jQuery("#searchbox").toggle("slide", { direction: "right" }, 500);
        return false;
    });
        });

jQuery(document).click(function() {
        jQuery("#searchbox").hide("slide", { direction: "right" }, 500);
 });

jQuery("#searchbox").click(function(e) {
    e.stopPropagation(); 
});

and here's a jsfiddle which I changed slightly for your purpose and for wordpress

Jalapeno Jack
  • 416
  • 7
  • 21
0

You could do it with a window.onclick event:
(You didn't post any HTML code so this is just an example of using it)

var yourbox = document.getElementById('yourbox');
window.onclick = function(event) {
    if (event.target == yourbox) yourbox.style.display = "none";
}
NullDev
  • 6,739
  • 4
  • 30
  • 54
0

If this is the full code, the error is caused by the extra '{' character you have in this function:

jQuery('body').on('click', function(e){
{
    $("#searchbox").toggle("slide", { direction: "right" }, 500);
});

You should remove the extra {

jQuery('body').on('click', function(e){
    $("#searchbox").toggle("slide", { direction: "right" }, 500);
});

That said, this logic might not work for what you're trying to do, but this is probably the unexpected token error.

Shilly
  • 8,511
  • 1
  • 18
  • 24