19
It is as follows, in jQuery we have the on
, any element <a>
with class test
and without the class foo
will trigger the function when clicked, even if you create the element later the event is already added:
$("#new").click(function () {
$('<p><a class="test" href="#">Novo: (' + (new Date) + ')</a></p>').appendTo("#container");
});
$("#container").on("click", "a.test:not(.foo)", function () {
console.log("Funcionou!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="new">Adicionar novo</button><br>
<div id="container">
<p><a href="#">Oi (não funciona)</a></p>
</div>
Note that in the test only the elements added later work, ie things like document.querySelector().forEach
will not work unless you use Mutationobserver, but then this would still be a third behaviour.
At first I thought jQuery wore Mutationobserver, but after a few tests I realized that actually the event really is in document
and #foobar
, then as I have custom to wear Vanilla.js I started trying to recreate this, so I used the Element.addEventListener
+Event.target
, was something like:
var container = document.getElementById("container");
var newBtn = document.getElementById("new");
on(container, "click", "a.test:not(.foo)", function () {
console.log("achou:", this);
});
newBtn.onclick = function () {
var n = document.createElement("p");
n.innerHTML = '<a class="test" href="#">Novo: (' + (new Date) + ')</a>';
container.appendChild(n);
};
function on(target, type, selector, callback)
{
target.addEventListener(type, function (e)
{
var el = e.target,
els = document.querySelectorAll(selector);
for (var i = 0, j = els.length; i < els.length; i++) {
if (els[i] === el) {
callback.call(el, e); //Passa o elemento como this e o event como primeiro argumento
break;
}
}
});
}
<button id="new">Adicionar novo</button><br>
<div id="container">
<p><a href="#">Oi (não funciona)</a></p>
</div>
However this does not seem to me very performatic, the doubt is the following:
- Is there any way to test a specific element with a queryselector?
In case, you’re trying to recreate the behavior of
on
jQuery with Vanilla?– Woss
@Andersoncarloswoss this "pure Javascript jquery.on", of course my example that works is not perfect, it is more a pseudo-code, but I will adjust it just to be aware
– Guilherme Nascimento
@Guilhermenascimento, I erased everything else, never mind. There was a print proving that I did not deny you. I don’t know if that’s what you want this time, I won’t post it as an answer, but that’s what I would reformulate, based on Sergio’s answer and the example function you gave - https://jsfiddle.net/vco5ptwm/1/ - I’m sorry I didn’t understand your question at first.
– Renan Cavalieri
I don’t want anything and whoever it is has already removed the downvote, understand one thing @Renancavalieri, I’m not against downvotes, as long as used properly, see this situation of my past where I received a downvote https://answall.com/questions/155013/o-que-e-para-que-serve-jwt/155045#comment320258_155045, although disagreeing with the downvote at that time, within the understanding of the Kaio it makes sense, so much even though my response suffered I supported. The problem was not the downvote, but his motivation, so I want when I receive a comment as criticizes:
– Guilherme Nascimento
Be receptive, patient and always assume good intentions. Until another big hug ;)
– Guilherme Nascimento