/    Sign up×
Community /Pin to ProfileBookmark

How to use event.stopPropagation(); ?

Hello,

I have searched the internet and found many examples, but I can’t figure out how to use stopPropagation() in my code.

On load there are multiple functions initialized.

[CODE]
$(document).ready(function() {

window.initializeFunctions = initializeFunctions() {
function1();
function2();
function3();
}

// Call the functions onload
initializeFunctions();

function1() {
$(‘li’).live(‘click’, function() {
alert(‘list item clicked’);
});
}

});
[/CODE]

Using AJAX I implement a new element and call [B]initializeFunctions()[/B]

[CODE]
// This is another .js file
$(‘#button’).live(‘click’, function(event) {

$.post(‘/get_list_item/’, {data: ‘data’},

function(data) {

$(‘ul’).append(‘<li>’ + data + ‘</li>’);

}, ‘json’);

// Call functions to the new element
initializeFunctions();

}
[/CODE]

HTML:

[CODE]<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>
<button>Click here</button>[/CODE]

So when clicking the button a new list item is appended to the list. I know you have to call functions again, to let them work with the new list item. However, the functions are also called on the other existing list items which let the click event trigger twice, when you click on one.

I know to use event.stopPropagation() somewhere, but I don’t know where 🙂

Looking forward to your replies!

Christophe

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Christophe27 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...