-1

For some reason, I cannot get my function to perform on the click of a button. I've learned mainly on codeacademy.com and unfortunately they don't give much real world application.

Here is my code:

<script>
    var ayee = function() {
        confirm("Ready to play?");
        var age = prompt("How old are you?");
        if (age >= 18) {
            document.write("Let's get started then!");
        }else{
            document.write("You're under 18? Be careful out there....");
        }
    }               
</script>
<button type="button" onclick="ayee" value="click" />

Much thanks.

2
  • Learn more about functions and event handling. Commented Dec 2, 2013 at 0:00
  • 1
    Please avoid using document.write after the page has already loaded... use innerHTML or standard DOM functions instead. Also, instead of using an onclick attribute, it's better to attach a click event handler to your button by giving it an id and using document.getElementById. Commented Dec 2, 2013 at 0:01

4 Answers 4

6

You need to invoke the function with (). Like this: ayee().

However, you shouldn't be using inline-js at all. Instead, you should do this:

Live demo here (click).

<button id="myButton">My Button</button>

JavaScript:

var myButton = document.getElementById('myButton');
myButton.addEventListener('click', ayee);

function ayee() {
  //do something
}

and don't use document.write here. You almost never need to use that, and if you do, be sure you really do.

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

13 Comments

Why not? Inline JS is less complicated and more compact.
@sirnomnomz stackoverflow.com/questions/19002690/… Read all of the answers/comments on this post, for starters.
@MistressDavid: In production code you should not use inline event handlers, it violates the principle of separation of concerns. You should not mix content with application logic. There are also some very confusing behaviors with inline event handlers, e.g. adding the properties of ancestor elements to the scope of the handler. That said, for just trying stuff out, it's OK (as long as you are aware of the quirks).
@MistressDavid aaaaand what happens when I want to change the name of the function I'm calling?
@MistressDavid IDs are supposed to be unique, there will only be one in the document, so I only need to change it in one place. What if I've got three dozen onclicks spread throughout my HTML? It's much easier to add a descriptive class and attach an event.
|
1

You are missing the () after ayee:

<button type="button" onclick="ayee()" value="click" />

Comments

0

Name the function

function ayee() {
  //insert your code
}

Call the function on click with the paranthesis

<button type="button" onclick="ayee()" value="click" />

This should do it.

Comments

0

You need brackets on the function call:

<button type="button" onclick="ayee()" value="click" />

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.