Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable mouse click when out of div

Tags:

jquery

Hi i have a div of a form. i want that disable click event when mouse is out of the div. So i tried this but it is not working ot of div is still clickable. Any idea??

var flag = false;
$("#foo").live("mouseenter",function(){
    flag = true;
}).live("mouseleave",function(){
    flag = false;
})

$(document).click(function(){
    if(!flag)
         return false;
});
like image 435
Sedat Başar Avatar asked Jul 12 '11 14:07

Sedat Başar


People also ask

How do I stop a div from clicking outside?

You can create a div with fixed position that spans the entire screen and place what you want to be able to click inside of it, making all the clicks outside that element actually be on that "empty" div. I also had to add z-index:10000 to deal with some unruly underlying components.

How do I turn off mouse click in CSS?

To disable a link using CSS, pointer-events property can be used, which sets whether the element in the page has to respond or not while clicking on elements. The pointer-events property is used to specify whether element show to pointer events and whether not show on the pointer.

How do I stop a click in HTML?

To disable a HTML anchor element with CSS, we can apply the pointer-events: none style. pointer-events: none will disable all click events on the anchor element.


1 Answers

You cannot prevent the click event being fired from the whole document. You can do it per element basis. You can probably block the whole screen using a absolute positioned transparent(low opacity) div and hide it again once the div is visible.

var $body = $(document.body);
var $div = $("<div id='dummyDiv'/>").hide().appendTo($body);
$div.css({position:"absolute", height: $body.height(), width: $body.width(), background: "#000", opacity: 0.5}).show(100);

//to hide it
$("#dummyDiv").hide(100);
like image 97
ShankarSangoli Avatar answered Sep 24 '22 02:09

ShankarSangoli