0

I have a widget in my page. If i click on any part of the widget it should navigate to next page which has some details about that widget. I need a dialog to be opened if i click on a span named "likes" available within that widget. But instead it is navigating to the next page even if i click on that link..

It should navigate to next page if i click on any part of the widget except that link..

This is normal code for click:

 $('td#' + parentElement).find('div.streamcontenttopmain').click(function() {
            window.location.href = 'TaskDetails.aspx?id=' + json_row.id + '&community=' + getQueryParam('community');
        });

This code will it navigate to next page if i click anywhere within the widget

My span is defined like this:

<span class="flft likes" style="MARGIN-LEFT: 2px">

This is What i tried:

 $('td#' + parentElement).find('div.streamcontenttopmain').click(function (e) {

            if ($(this).hasClass("flft likes")) {

                alert("hi");
            } else {
                window.location.href = 'TaskDetails.aspx?id=' +
                               json_row.id +
                               '&community=' +
                               getQueryParam('community');
            }
        }); 
1
  • I posted an answer using the condition. The problem is that even when you click the span the event won't be handled until it's propagated to the 'div.streamcontenttopmain'` element at which point thiswill be the div and not the span Commented Oct 5, 2012 at 8:05

3 Answers 3

2

You can stop the event from bubbling up to the div elements and triggering their event handlers:

$('#' + parentElement).find('span.likes').click(function(e) {
    e.stopPropagation();

    // ...
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can also do something like :

$(document).ready(function(){
    $('div').click(function(e){
        if(e.toElement.nodeName == "SPAN")
            alert('span clicked');
        else
            alert('div clicked');
    });
});

I showed it in this demo: http://jsfiddle.net/SxhWy/3/

3 Comments

I have tried the way u mentioned.. But it is throwing an exception as: "console is undefined"
@Xavier I deleted this line, It was only needed for me to trace the exact names of fields.
@RuneFS I will try to be more explicit.
1

If you stop the propagation of the event you can handle it in the span wihtout it ever reaching the div. That woul look similar to the below

$('td#' + parentElement).find('div.streamcontenttopmain').click(function(e){
      window.location.href = 'TaskDetails.aspx?id=' + 
                               json_row.id + 
                               '&community=' + 
                               getQueryParam('community');
})).find('span.likes').click(function(e) {
           e.stopPropagation();
           //do the span specific stuff here
});

2 Comments

I have edited my question in the way u mentioned.. I have tried in the same way but still it remains the same.. Should i add anything more...
@Xavier the original answer I wrote was a blunder (see comment to question for why) this which expands on Blenders answer will however work if I've understood your question and context correct

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.