Forum Moderators: open
// HTML
<button class="button"
onClick="if (checker()) console.log('returned')">
Go
</button>
// In a separate JavaScript .JS
$('.button').on('click', function(e) {
console.log('onclick');
});
function checker() {
console.log('function');
return true;
}
<button class="button">Go</button>
<button class="button">Go 1</button>
<button class="button">Go 2</button>
<script>
(function( d ) {
'use strict';
var but=d.querySelectorAll( '.button' );
for ( var c=0; c < but.length; c ++ ){
but[c].addEventListener( 'click',
function(e){
console.log( 'onclick');
if ( checker() ) {
console.log( 'returned' );
};
},false);
}
function checker() {
console.log( 'function' );
return true;
}
}( document ));
</script>
// Inline on the HTML page
<script>
function checker() {
console.log('function');
}
</script>
// HTML
<button class="button" data-callback="checker">
Go
</button>
// In a separate JavaScript .JS
$('.button').on('click', function(e) {
console.log('onclick');
if (typeof window[$(this).data('callback')] == 'function')
window[$(this).data('callback')]();
});