document on click dynamic element
$(document).on('click', '.class', function() {
// do something
});
document on click dynamic element
$(document).on('click', '.class', function() {
// do something
});
jquery dynamic event handling
var counter = 0;
$("button").click(function() {
$("h2").append("<p class='test'>click me " + (++counter) + "</p>")
});
// With on():
$("h2").on("click", "p.test", function(){
alert($(this).text());
});
how to create dynamic onclick event in javascript
/* This example assumes you have two elements with an id
attribute of 'cats' and 'dogs'.
*/
const conditionOneHandler = event => {
alert('I like cats!');
}
const conditionTwoHandler = event => {
alert('I like dogs!');
}
const clickHandler = (event) => {
// Do whatever. You can use the target object
// from the event to conditionally handle the
// event.
switch (event.target.id) {
default: return;
case 'dogs': return conditionOneHandler(event);
case 'cats': return conditionTwoHandler(event);
}
}
/*
Attach the click event listener to the element.
You can also use a comma after each line to declare
multiple variables of the same type, instead of
writing 'const' every time. :)
*/
const dogsEl = document.querySelector('#dogs'),
catsEl = document.querySelector('#cats'),
dogsEl.onclick = event => clickHandler(event),
catsEl.onclick = event => clickHandler(event)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us