2

I am in my second semester at SCSU and currently taking an introductory course into Computer Science. We are going over the basics of HTML and Javascript.

The latest lab I worked on was using a button to change a sad face into a happy face. However, I was wondering if it was possible to check if you have already clicked the button, if so, using an If statement it will pop up an alert saying, "You made me smile!"

<html>
<head>
<title>Are you sad?</title>
</head>

<body>

<div style="text-align:center">
<img id="sadFace" src="http://balance3e.com/Images/sad.gif">
<p>

<input type="button" value="Smile"
    onclick="document.getElementById('sadFace').src='http://balance3e.com/Images/happy.gif'; 
    alert('You made me smile!');">
</p>
</div>

</body>
</html>

Presently the program will display the alert when I click the button (this was for extra credit). However, I would like to check if the button has already been pressed, if so it will display the alert.

1
  • 3
    if(document.getElementById('sadFace').src == 'http://balance3e.com/Images/happy.gif'){alert('clicked')}. I hope that answer your question Commented Feb 13, 2014 at 18:49

5 Answers 5

2

I would do it like this:

<html>
<head>
    <title>Are you sad?</title>

    <script>

        function changeFace() {

            if(document.getElementById('sadFace').src == 'http://balance3e.com/Images/happy.gif') {
                alert('You made me smile!');
            }

            document.getElementById('sadFace').src='http://balance3e.com/Images/happy.gif';

        }

    </script>

</head>
<body>

    <div style="text-align:center">
    <img id="sadFace" src="http://balance3e.com/Images/sad.gif">
    <p>
        <input type="button" value="Smile" onclick="changeFace()">
    </p>
    </div>

</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response. I like all the different ways of solving the same problem.
1

Here is my way:

onclick= "if( i== 0)
         {
           document.getElementById('sadFace').src='http://balance3e.com/Images/happy.gif'; 
           i++;
          }
          else
          {
             alert('You made me smile!');
          }"

Of course you will have to define the variable i and set its value to zero at first.

2 Comments

@mccdlibby I would just note that it's bad practice to put JavaScript right in your HTML, especially when it's this long. I know this is just an exercise, but just so you know...
Thank you for the advice - I will keep this in mind for future reference!
1

You need to create a variable that will keep track of whether this button was pressed. Maybe call this variable "isPressed" and set its default value to false. When you click the button, if the variable's value is false, set it to true. Otherwise, alert out your message.

Comments

1
<html>
<head>
<title>Are you sad?</title>
</head>
<script>var i =0;</script>
<body>

<div style="text-align:center">
<img id="sadFace" src="http://balance3e.com/Images/sad.gif">
<p>

<input type="button" value="Smile"
    onclick="
    if(i == 0 ){
i=1;
document.getElementById('sadFace').src='http://balance3e.com/Images/happy.gif'; 
alert('You made me smile!');
}">
</p>
</div>

</body>
</html>

Comments

1

Try

onClick = "if(document.getElementById('sadFace').src ==                 
            'http://balance3e.com/Images/happy.gif')
           {
              alert('You made me smile!');
           }"

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.