JavaScript Javascript - Checking button clicks with If Statement javascript checking radio button value,javascript checking radio button,javascript

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.

Answer:1

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>
Answer:2

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.

Answer:3
<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>
Answer:4

I want to reset my form having id user_post. This form contains hidden fields also. I used this code to reset input form fields $('#user_post').each(function(){ this.reset(); }); My form given ...

I want to reset my form having id user_post. This form contains hidden fields also. I used this code to reset input form fields $('#user_post').each(function(){ this.reset(); }); My form given ...

  1. arena
  2. arena football
  3. arendelle
  4. arena stage
  5. arenado
  6. arenal volcano
  7. arena sports
  8. arent
  9. arenal costa rica
  10. arena theatre
  11. arena of valor
  12. arena stage dc
  13. arena sf
  14. arent fox
  15. arena ktown
  16. arenanet
  17. arenado trade
  18. arenacross
  19. arenal volcano national park
  20. arena theater

In my Node.js app I have a database layer that calls toString on input parameters that need to be passed to the database as a string (for example, numbers). The places where I pass parameters to the ...

In my Node.js app I have a database layer that calls toString on input parameters that need to be passed to the database as a string (for example, numbers). The places where I pass parameters to the ...

  1. calling tostring on string java
  2. calling tostring on string
  3. calling tostring on string c#
  4. avoid calling tostring() on string objects this is unnecessary
  5. avoid calling tostring() on string objects

Here is my code, but it's not working. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3....

Here is my code, but it's not working. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3....

function showwaitmsg() { $("#wait").fadeIn(); return true; } function hidewaitmsg() { $("#wait").fadeOut(); } $("a:contains('Test1')").bind("click", function ...

function showwaitmsg() { $("#wait").fadeIn(); return true; } function hidewaitmsg() { $("#wait").fadeOut(); } $("a:contains('Test1')").bind("click", function ...

  1. make image visible using javascript
  2. make div visible using jquery
  3. make control visible using javascript
  4. make checkbox visible using javascript
  5. make panel visible using javascript
  6. make panel visible using jquery
  7. make control visible using jquery
  8. make textbox visible using jquery
  9. make textbox visible using javascript
  10. make button visible using jquery
  11. make image visible using jquery
  12. make label visible using jquery
  13. make a div visible using javascript
  14. make asp panel visible using javascript
  15. make a label visible using javascript
  16. make sentences using visible
  17. make asp.net control visible using javascript