ItsShowtime
ItsShowtime

Reputation: 193

onclick attribute can't find Javascript function

I can't manage to link my Javascript function to my 'onclick' html attribute in the span tag.
I keep getting the error:

"Uncaught ReferenceError: closeAndRefresh is not defined" "at HTMLSpanElement.onclick"

My code looks like this:

<div class="error">
    <div id="errorMessage">
        <span onclick="closeAndRefresh();">&#10006;</span>
        <p id="message">
            <?php echo $confirmationMessage ?>
        </p>
    </div>
</div>


<script>
    function closeAndRefresh() {
        <?php $visibility = 'hidden' ?>
        window.location.reload(true);
    }
</script>

I'll also add a screenshot of my code:

enter image description here

Upvotes: -1

Views: 15754

Answers (4)

Giorgio Barchiesi
Giorgio Barchiesi

Reputation: 6177

If you have recently added the function to your javascript code, consider clearing the browser's cache, in order to force it reload the javascript code. To check if this is the problem, on your browser show the source html code, find the line where the javascript code is included, and click on it; it will show you the javascript source code the browser is currently using, that may be out of date. Also, as mentioned by others, make sure you include the javascript code before you reference it.

Upvotes: 1

onkar sawant
onkar sawant

Reputation: 9

Try writing the script function closeAndRefresh() in the head tag of your html page.

Maybe the html page is not able to load the script when you write it inside the body.

Upvotes: 0

Murali N
Murali N

Reputation: 3508

Make sure you reference the function correctly and also try putting the script above or below as it seems to be function not recognized.

Upvotes: -1

Gaurav Chaudhary
Gaurav Chaudhary

Reputation: 1501

Your script is after your HTML. Include it before html so that your function is defined

function closeAndRefresh(){
  
        window.location.reload(true);
}
<span onclick="closeAndRefresh();">&#10006;</span>

Upvotes: 4

Related Questions