I usually don't have a problem using JQuery, but sometimes when I write standalone Javascript I feel like a complete idiot, like today. (I'm doing this simply because one I'm bored and two I'm trying to understand Javascript more itself.)
Here's the JQuery...
$(document).ready(function() {
$(".call").on('click', function() {
if ( $(".code").val() === "" ) {
alert("Function denied!");
} else {
alert("Function accepted!");
}
});
});
And here is about 30 minutes of toying around without getting it working, until I put it right before the closing </body>
tag.
Can someone explain why it has to be done this way and not in the head tags?
var code = document.querySelector('.code'),
call = document.querySelector('.call');
document.addEventListener('DOMContentLoaded', function() {
call.addEventListener('click', function() {
if ( code.value === "" ) {
alert("Function denied!");
} else {
alert("Function accepted!");
}
});
});