0

I have a PHP page in which am hiding a div using a javascript method. This method is called on a hyperlink click. The issue is its giving Uncaught Reference error. Not sure what is the cause. I tried even to have a external JS file and define the method in that, but still not working.

1
  • 9
    No way to help if you don't post any of your code... Commented Jan 29, 2013 at 7:17

3 Answers 3

1

I'll give you a hint:

make sure you have defined your DIV tag before the action script

like:

<html>
 <head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div id="hideDiv">Hide on click</div>
<input type="button" value="hide" onClick="javascript:HideIt()"/>
<script type="text/javascript" language="javascript">

function HideIt(){
 //what ever it is:Hide codes goes here
 $('#hideDiv').hide() // we used it after the defined it :)
 }
</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Uncaught Reference error in javascript usually means that the object or accessor isn't defined at the time of calling the method.

I've seen it when using jQuery when jQuery is included at the bottom of the html and the javascript is being triggered before it.

So

$('divid').text = "blah";

before the

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

Hence the $ method doesn't exist.

The solution would be to put the method call on body load.

Comments

0

Here is the general code to hide any div onClick.

<html>
    <head>
        //Here to add script tag, which i have writen below
    </head>
    <body>
        <div id="testDiv">This is to be hide on click</div>
        <br/>
        <br/>
        <a href="#" onClick="$('#testDiv').hide();">Click to hide!</a>
        <br/>
        <br/>
        <a href="#" onClick="$('#testDiv').show();">Click to show!</a>
    </body>
</html>

NOTE: You should write apostrophes carefully. Bcox ' and " have different impact

Here is the script tag which u have to place in head tag:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

Comments

Your Answer

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.