Lets break this down sample by sample
<button onclick="{alert($(this).text());}">Testing</button>
This would be same as doing the following in pure javascript.
document.querySelector('button').addEventListener('click', function() {
{
alert($(this).text());
}
});
It's a bit weird to add extra braces like that but it is fully allowed and the code within will be executed. For your second sample this gets really weird though.
<button onclick="function(){alert($(this).text());}">Testing</button>
This would be the same as this.
document.querySelector('button').addEventListener('click', function() {
function() {
alert($(this).text());
}
});
In other words, when you click on the button you'll declare a new function but you'll never call it. To get around this you would need to wrap it in paranthesis and then call .call(this)
on the function to execute with the same this
as the caller.
document.querySelector('button').addEventListener('click', function() {
(function() {
alert($(this).text());
}).call(this);
});
Or in your style.
<button onclick="(function(){alert($(this).text());}).call(this)">Testing</button>