Friday, June 15, 2018

Pure JavaScript instead of jQuery - ready, on, click and end selector




I have in jQuery:



$(document).ready(function() {
$(document).on('click', "[id$='someId']", function () {

console.log($(this).val());
})
})


How can I write it in pure JavaScript?




$(document).ready(function()





Should I use "ready" in pure JavaScript?




$(document).on




How can I use "on"? I have to add dynamic elements, so in jQuery I must use "on".



I would like write this in accordance with ES6.



Answer



Use addEventListener instead of on:



document.addEventListener('DOMContentLoaded', function () {
document.body.addEventListener('click', function (e) {
if (e.target.matches("[id$='someId']")) {
console.log(e.target.value);
}
});
});



The event delegation makes it a bit more complex, but it should do the trick.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...