0

Hi i always use this example code to make a div work as link.

<div onclick="location.href='http://www.example.com';" style="cursor:pointer;"></div>

The problem is i have inserted an other javascript action inside (this action need to stay on the current page) the problem is Not the first click but the second..

This javascript actions its an ajax function that "change" that html.. in the fiddle where i have no ajax, its working great, on first, second, third, any clic..

Here is the code http://jsfiddle.net/HzsH9/4/

Im using.. Jquery, also this is the anti propagate code im using

$("a").bind("click", function(e){ alert("clicked!"); e.stopPropagation() });

The outer div class is class="listingsRow"

and the inside javascript goes here

<a id="btn_remove_114" name="btn_remove_114" onclick="ajaxFavouratesRemove(1,114,375);">
                           <div class="fav"></div></a>

After ajax success, its changed for this

 <span id="spadd114"><a id="btn_add_114" name="btn_add_114" onclick="ajaxFavouratesAdd(114);"><div class="nofav"></div></a></span>

Also i just found this, but i cant manage to do the same how to stop event propagation with slide toggle-modified with the updated code.

Community
  • 1
  • 1
Moncho Chavez
  • 694
  • 2
  • 10
  • 31
  • click events propagate _down_ the DOM, so it's not surprising it reaches the `div` element before the `a` element. – Aurelia Nov 20 '13 at 09:10
  • what? no. events propagate up. and eventually reaches window. – monopoint Nov 20 '13 at 09:19
  • I'm sorry, but I don't understand the question. could you try to be a bit more clear as to what you want? – monopoint Nov 20 '13 at 09:27
  • In your fiddle, it seems to be acting in the way you want it to. The click event on the yellow box doesn't propagate beyond the `` tag. – Khior Nov 20 '13 at 09:29

2 Answers2

3

Classic case of event delegation

$(".listingsRow").on('click','a',function(e){ 
  alert('clicked');
  e.stopPropagation();
})
Fikri Marhan
  • 359
  • 1
  • 9
2
$('#singles_114').click(function(e){
    e.stopPropagation()
});
  • Thanks but for each item i need a different id than 114? is there a way to set for "anynumber" – Moncho Chavez Nov 20 '13 at 16:56
  • declare all items with different ids like 114,115..etc, and on the click call the same function, next inside that function using jquery attr() get the id and use it like based on your requirement. Sorry if my english is bad. – Suganthan Madhavan Pillai Nov 20 '13 at 17:11