1

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

BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
asked Jan 10, 2013 at 19:24
1
  • Why $(function($){...})? Commented Jan 10, 2013 at 19:27

3 Answers 3

5

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.

answered Jan 10, 2013 at 19:25
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, yes, thank you. And one more question, now browser say me that "Uncaught TypeError: Object #<HTMLLIElement> has no method 'hide' "
4

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.

answered Jan 10, 2013 at 19:31

Comments

0

Syntax is wrong. Replace the your javascript(mentioned above) with

 function testFunc(obj) {
 $(obj).hide();
 }
answered Jan 10, 2013 at 19:41

2 Comments

How is your answer any different from bfavaretto's?
@j08691: There is a difference. The OP had obj.hide(), as opposed to $(obj).hide(). The former won't work, since obj is a DOM element, not a jQuery object.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.