-1

I am currently working on a school project and one of my functions is not running, onclick. I dont know why. I know its the function because i added a console.log() command.

HTML:

<div id="popup">
        <div id="contain">
            <div id="advert"><a href="#" onclick="close()" id="close">close</a></div>
        </div>
        <div id="block"></div>
        </div>

JS:

function close(){
                console.log("worked");
                $("#popup").hide();
            };
2
  • 3
    close() as a global is already taken by the browser. Try renaming the function to closePopup() or similar to avoid the conflict. Commented May 29, 2016 at 5:02
  • 1
    This is one reason why you shouldn't use inline event handlers in global scope. Commented May 29, 2016 at 5:09

2 Answers 2

3

function closePopup(){
  console.log("worked");
  $("#popup").hide();
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="popup">
        <div id="contain">
            <div id="advert"><a href="#" onclick="closePopup()" id="close">close</a></div>
        </div>
        <div id="block"></div>
</div>

Your code didn't work because, close is an inbuilt function. Changing the name of the function do the trick.

Sign up to request clarification or add additional context in comments.

Comments

0

You named your function close, which is reserved for window.close() and that is why it is not working. Just name it to something else and it will work :D cheers!

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.