I have a function in file.
<script type="text/javascript" src="domain.com/js/script.js"></script>
This is just script from head:
$(function($){
function testFunc(obj) {
obj.hide();
}
});
In index.php:
<li onclick="testFunc(this)">text</li>
And nothing change, browser (Chrome) said:
Uncaught ReferenceError: testFuncis not defined
3 Answers 3
You have a scope issue, just remove the function from the document.ready block:
//$(function($){
function testFunc(obj) {
obj.hide();
}
//});
When you define a function inside of another function, it will only be visible inside of that function.
1 Comment
The better way to use jQuery to do this is:
$(function($){
$('li').click(function(){
$(this).hide();
});
});
and remove the inline click handler you currently have.
Comments
Syntax is wrong. Replace the your javascript(mentioned above) with
function testFunc(obj) {
$(obj).hide();
}
2 Comments
obj.hide(), as opposed to $(obj).hide(). The former won't work, since obj is a DOM element, not a jQuery object.
$(function($){...})?