I have 3 Files:
Php file where i include the other two files like this:
<head>
<!-- 1. Javascript File-->
<script src="js/functions.js"></script>
</head>
<html>
<?php include 'includes/question1.html' ?>
</html>
The functions.js file looks like this:
$(document).ready(function(){
function showError(){
alert('ERROR');
}
});
$('input').change(function(){
// SOME STUFF HERE
});
But Now i want to use the "showError" Function or the "change" Function in the third data or in the normal Php file but it says everytime that there is no function like this.
It's only a problem of visibility how can i change this ?
The problem doesn't have anything to do with the files, they're fine if jQuery is found and the js is executes (which seems to work as you get an error from it).
Your problem is the scope in which showError
is available. You've declared it inside the jQuery-ready handler, which means that it is only available in there and not from the outside.
Also, you should put DOM selections like $('input')
always inside the ready handler - that's what it was made for.
So this will work:
$(document).ready(function(){
function showError(){
alert('ERROR');
}
$('input').change(function(){
// SOME STUFF HERE
// including calls to showError();
});
});
Actually, you could even move the showError
declaration out of the handler into the global scope.