10

Ok, simple question:

<div onclick="javascript:manualToggle(this)">
    <span>Allowed to click</span>
    <span>Not allowed to click</span>
    <span>Allowed to click</span>
</div>

Without replicating the manualToggle on to the 2 spans that are allowed to click, how can I prevent the "Not allowed to click" span from firing it's parent div onclick event when it is clicked on?

7 Answers 7

21

Give the span an id an attach onclick event to it and use

A jQuery sample

$("#spn2").click(function(event){
  event.stopPropagation();  
});

event.stopPropagation(): Stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

Sign up to request clarification or add additional context in comments.

2 Comments

I went for something similar, but not with stopPropagation. (I haven't down voted anything in this question...)
There's a difference between a correct answer and a solution that worked. This is actually a correct answer to this question. Thanks!
9

It would make sense to use a library but without you can try this (edited with an entire page to test):

<html><head></head>
<body>
<script type="text/javascript"><!--
function manualToggle(val)
{
    alert(val.id);
}

--></script>

<div id="test" onclick="manualToggle(this);">
    <span>Allowed to click</span>
    <span onclick="event.cancelBubble=true;if (event.stopPropagation) event.stopPropagation();">Not allowed to click</span>
    <span>Allowed to click</span>
</div>
</body>
</html>

2 Comments

event? e? there seems to be missing something
Thx, e doesn't work I only tested it in a cancelBubble Browser, I changed it to event (which is window.event) and put some testing code in there. It works at least in FF 3.5 and IE7
2

You need an event handler (it's very easy to do this in something like jQuery) that catches clicks for the spans within the div and only fires the function if, for example, the span has/hasn't a particular class.

Comments

2

I just had the same issue and could not get jQuery to work so I used simple Javascript:

document.getElementById("your_span").addEventListener("click", (event) => {
    event.stopPropagation();
});

That did the trick for me. Obviously you need to add the addEventListener to every Element you wanna apply this to. Since I do a lot of DOM manipulation this was not an issue for me. Hope this helps anyone :)

Comments

1

with mootools you can use the method stopPropagation:

$('myChild').addEvent('click', function(ev){
    ev.stopPropagation(); // this will prevent the event to bubble up, and fire the parent's click event.
});

see http://mootools.net/docs/core/Native/Event#Event:stopPropagation

also see this very similar question: How can I stop an onclick event from firing for parent element when child is clicked?

3 Comments

downvote prolly for the js-library you suggest, just like phoenix who suggest jQuery
using libraries is the easiest (and most browser-compatible) way
I used the accepted answer to swap a few bits round and use jQuery to achieve what I needed. Thank you for your answer though. (I haven't down voted anything in this question...)
0

Possible solution: give the span's id's and check whether the clicked id is allowed to be clicked in your function

bad idea: you don't know which span is clicked since you call the function from your div...

Comments

0
<div onclick="manualToggle(this)">
    <span>Allowed to click</span>
    <span>Not allowed to click</span>
    <span>Allowed to click</span>
</div>
<script>
    function manualToggle(cur){
      if(cur !== event.target) return false;
       
       //CODE
    }
</script>

Here we have set a click event on div tag, and we are passing the current element(div) as parameter

inside the manualToggle function you have the element in params where you have set the event,

inside the function we have event (a global var object), where you can get the clicked element (event.target),

if the clicked element is not same(equal) to the element where we have set the event then do nothing.

there are some other methods are also available, use stopPropagation

https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation

2 Comments

Its always helpful to add description with the code for better understanding. Please read stackoverflow.com/help/how-to-answer
oh alright thanks for that, lemme edit this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.