Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent clicking through div

Tags:

html

css

I have a div container, which holds several "rows" of data, with each item in the list in it's own "listRow" div. I'm using jQuery to make the "listRow" divs selectable, by toggling the css styling when clicked. Also within each row is an image div that floats on the right side of it's parent "listRow" div and contains an onclick event. When I click on the image div, the onclick event fires, but the parent "listRow" div is also clicked and "selected".

How can I prevent from clicking through my image div?

pseudo html code:

<div class="listContainer">
    <div class="listRow" id="listRow1">
        <div class="listItemName">List item #1</div>
        <div class="listItemIcon"><img src="images/icon.png"></div>
    </div>
    <div class="listRow" id="listRow2">
        <div class="listItemName">List item #2</div>
        <div class="listItemIcon"><img src="images/icon.png"></div>
    </div>
</div>

jQuery code:

$("div.listRow").click(function(){
    $(this).toggleClass("selected");
})
like image 526
xPox Avatar asked Dec 12 '13 14:12

xPox


People also ask

How do I stop div clicking?

To disable clicking inside a div with CSS or JavaScript, we can set the pointer-events CSS property to none . Also, we can add a click event listener to the div and then call event. preventDefault inside.

How do I restrict clicking events in HTML?

You can't prevent click with css, you must use javascript. The dislpay:none only hide a element, in this case, a div.

How do you stop a click event in CSS?

To disable the click event on an element, we can use the pointer-events property. Simply set the pointer-events property to none to disable the click event on any element that you want. In the following example, we have disabled the click event for a button by simply setting its pointer-events property to none .

How do I make a div transparent in click?

You can solve this problem using the “none” value of the CSS pointer-events property. In the case of using this value, the element will not react to the pointer-events. With the help of the CSS background property put your transparent background.


2 Answers

You'd use event.stopPropagation():

event.stopPropagation() - Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

On the div.listItemIcon's click() event to prevent the event bubbling:

$('div.listItemIcon').click(function(e){
    e.stopPropagation();
});

$("div.listRow").click(function(){
   $(this).toggleClass("selected");
});

jsFiddle example here.

like image 74
dsgriffin Avatar answered Oct 24 '22 12:10

dsgriffin


In the click function of the image if you grab the event and use the stopPropagation() function then it should keep the event from firing the the element's parents. eg.

$('div.listItemIcon').click(function(e){
    //your code here
    e.stopPropagation();
});
like image 22
Don Avatar answered Oct 24 '22 12:10

Don