5

I wonder what I am doing wrong in this case? I got this button which is an HTML element. When I click on it, I want the function push() to get activated. Unfortunally my console says:

ReferenceError: push is not defined

Can some one help me in this case? Thanks so much!

<button type="button" onclick=push()>Click Me!</button> 
<script type="text/javascript">
function push (){.....};
</script>

1
  • Can you provide the content of the push() function? Commented Aug 7, 2017 at 21:59

2 Answers 2

11

You can change onclick=push() to onclick="push()" to make it work!

Also, there might be something wrong in the body of the push function because of which it is not able to compile - hence, the error.

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

1 Comment

If there's no white space after the = than the quotes aren't necessary
-1

You could do these instead :

You can do this if you want to use the function push() with only one button,

<button type="button" id="clickme">Click Me!</button> 

<script type="text/javascript">

document.getElementById("clickme").addEventListener("click", function() {.....}

</script>

And this if you want to use the function push() with multiple button,

<button type="button" id="clickme">Click Me!</button> 

<script type="text/javascript">

document.getElementsByClassName("clickme").addEventListener("click", function() {.....}

</script>

Pros : Less HTML Cons : More javascript

2 Comments

Isn't getElementById the better choice in this case?
@Suppe Yes it is, class is for multiple elements, and id for only one element, so in this case it's GetElementById

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.