3

I have a popup div that shows only when clicked on particular button. It even hides when clicked on the same button. My problem is, i also want to hide div when clicked anywhere outside. I am not able to do so because the popup div is inside the main wrapper class and can't do so by using click event on the wrapper class and making it hide. This is my code:

function showHide() {
    var ele = document.getElementById("div_fieldWorkers");

    if (ele.style.display == "block") {           
        ele.style.display = "none";
    } else {
        ele.style.display = "block";      
    }
}

<input type="button" value="Add Field Worker" id="btnFieldWorkers" onclick="return showHide();" class="btn btn-primary" />
Hannes Johansson
  • 1,794
  • 2
  • 15
  • 28
Manu
  • 418
  • 3
  • 8
  • 20

6 Answers6

1

The solution to this problem is here: Use jQuery to hide a DIV when the user clicks outside of it

Also, you tagged this question jquery, but your code is pure javascript. When using jQuery, you can write only

$('#div_fieldWorkers').toggle();

in your onclick.

Community
  • 1
  • 1
lort
  • 1,458
  • 12
  • 16
  • yeah.. but it didn't work with this code. problem is that the popup div is inside the main wrapper class. Whenever i fire click event outside the div(i.e. on wrapper class) to hide it, it doesn't work at all. It gives priority to the wrapper class click event and thus the div always remain hidden. – Manu Sep 30 '13 at 11:16
1

Check this out: http://jsfiddle.net/d4SsZ/1/

Revised: http://jsfiddle.net/d4SsZ/3/

Just a snippet: Validate for null and undefined js errors if any.

Markup:

<div id="div_fieldWorkers" class="form_size" style='display:none;' class='noclick'><span class='noclick'>Hello How are you?</span></div>
<input
    type="button"
    value="Add Field Worker"
    id="btnFieldWorkers"
    class="btn btn-primary" />

Javascript:

 $('#btnFieldWorkers').bind("click", ToggleDisplay);

function ToggleDisplay() {
    if ($("#div_fieldWorkers").data('shown'))
        hide();
    else 
        display();
}

function display() {    
    if ($("#div_fieldWorkers").children().length > 0) {
        $("#div_fieldWorkers").fadeIn(500, function() {
            $(document).bind("click", function() {hide(); });            
            $("#div_fieldWorkers").data('shown', true)});         
    }  
}

function hide() {   
    if (window.event.toElement.className != 'noclick') {
        $("#div_fieldWorkers").fadeOut(500, function() {
            $(document).unbind("click");
            $("#div_fieldWorkers").data('shown', false);                
        });
    }
}
Vishal Vaishya
  • 596
  • 3
  • 15
  • when i click inside the div.. it gets hidden.. i want if the user clicks inside the div.. it doesnot hide itself – Manu Sep 30 '13 at 11:33
  • @user2829939 Check this http://jsfiddle.net/d4SsZ/3/ Just added class as noclick and onhide skipped the click event. There may be a good approach than this. For time being: it will work for you. – Vishal Vaishya Sep 30 '13 at 13:12
1

Had the same problem, came up with this easy solution. It's even working recursive:

$(document).mouseup(function (e)
{

var container = $("YOUR CONTAINER SELECTOR");

if (!container.is(e.target) // if the target of the click isn't the container...
    && container.has(e.target).length === 0) // ... nor a descendant of the container
{
    container.hide();
}
});
Waseem Khan
  • 127
  • 1
  • 6
0

Create click event on body element and stopPropagation. this makes the event to call the click event only on the button. Then check that the click target element isn't the popup div. example:

$(function(){
   $("#btnFieldWorkers").click(function(e){       
       e.stopPropagation();
       $("#div_fieldWorkers").show();
       $("body").click(function(e){
           if(e.target.id != "div_fieldWorkers")
           {
               $("#div_fieldWorkers").hide();
               $("body").unbind("click");
           }
       });
   });

});

jsfiddle

Dvir
  • 3,287
  • 1
  • 21
  • 33
  • when i click inside the div.. it gets hidden.. i want if the user clicks inside the div.. it doesnot hide itself as it contains a form to be filled by the user – Manu Sep 30 '13 at 11:50
  • see the jsFiddle link. It's not supposed to disappear it. show me your code – Dvir Sep 30 '13 at 14:46
0
document.addEventListener('click', function () {
  document.querySelector('.menu').classList.remove('active');
});

document.querySelector('.toggle-menu-btn').addEventListener('click', function (event) {
  document.querySelector('.menu').classList.toggle('active');
  event.stopPropagation();
});

document.querySelector('.menu').addEventListener('click', function (event) {
  event.stopPropagation();
});
Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
-1

You can also hide the pop up by click on pop up div.For this you have to provide click function in the div tag.In that click function you have to write the close pop up functionality.