2
Aren’t you supposed to get the same result using one method or the other? I would add one Listener oncick
and only using the second method could he effectively declare Event Listener. Why?
var record = document.querySelector('#record');
record.onclick = function(){
alert("Entrou!");
}
var record2 = $('#record2');
record2.onclick = function(){
alert("Não vai entrar!");
}
<!--UPDATE-->
var record3 = $('#record3');
record3.on('click', function(){ alert('Vai entrar sim!'); });
<script
src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
crossorigin="anonymous"></script>
<button type="button" id="record">Record</button>
<button type="button" id="record2">Record 2</button>
<!--UPDATE-->
<button type="button" id="record3">Record 3</button>
The former uses the alias for jQuery, while the latter uses the DOM with Javascript only.
– UzumakiArtanis
and what a difference that makes at the level of
onclick
?– ihavenokia
If you create a [mcve] with the described behavior it will be easier to infer what is occurring.
– Woss
@Andersoncarloswoss made!
– ihavenokia
@ihavenokia if I’m not mistaken (I might be wrong because I’ve never tested), you’re trying to use the
jQuery
to select, and the onclick ofJS
to click onjQuery
would just$('#record2').click(function(){})
– Rafael Augusto
record2.on('click', function(){ alert('Vai entrar sim!'); });
– Renan Gomes
Added the above example to Fiddler
– ihavenokia
fixed, and works!
– ihavenokia