I've read many things about $(document).ready(function() it's clear when it is usefull , so generally i write $(document).ready(function() inside
but , why the devil , why is it KO in this so simple case ? if you just put off le $(doc.... it works perfectly
code :
<?php
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script defer>
$(document).ready(function(){
//alert('ready boy');
function changeText(id) {
id.innerHTML = "Ooops!";
}
});
</script>
</head>
<body>
<h1 onclick="changeText(this)">Click on this text!</h1>
</body>
</html>
It's a scope problem. When using the outdated
on*event attributes the function you call must be defined under the scope of thewindow. In your code example you've defined it within the scope of jQuery's ready handler.To fix this you can either move the function to be directly within the
<script>tag, and hence under thewindowscope:Alternatively, and preferably, you could remove the
on*event attribute and attach your event handler using unobtrusive Javascript instead: