Javascript Button

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Javascript Button

Post by TheBon on Thu Dec 16, 2010 2:08 am
([msg=50522]see Javascript Button[/msg])

I cannot figure out how to create a button, that when clicked will display a pop-up box that shows a conditional statement.
The conditional statement is determined from a random number using: Math.random();

I want the pop-up to display the conditional statement using the following:

Code: Select all
var x=Math.random();

if (x>=0.3)
{alert(True);}
else
{alert(False);}


That's what I have right now. But i don't want it to alert me when the page loads, I want it to alert me when the button is clicked.

Any help would be appreciated.
TheBon
New User
New User
 
Posts: 2
Joined: Thu Dec 16, 2010 2:05 am
Blog: View Blog (0)


Re: Javascript Button

Post by OnlyHuman on Thu Dec 16, 2010 3:12 am
([msg=50524]see Re: Javascript Button[/msg])

The simplest way I can think of is just to encompass that bit of script you already have, within the body of a function, and then call that function from an HTML button input. Something like this maybe?

Code: Select all
<html>

<head>

    <script type="text/javascript" language="javascript">

        function check()
        {
            var result = (Math.random() >= 0.3) ? "True" : "False";
            alert( result );
        }

    </script>

</head>

<body>

    <input type="button" value="Press Me" onclick="check();" />

</body>

</html>

I rewrote your code just a bit. It still does the same thing, just in a different way. There wasn't anything wrong with what you had, I just like to add my own touch to things sometimes.
OnlyHuman
Poster
Poster
 
Posts: 192
Joined: Sat Aug 22, 2009 1:37 am
Blog: View Blog (0)


Re: Javascript Button

Post by thetan on Thu Dec 16, 2010 10:42 am
([msg=50532]see Re: Javascript Button[/msg])

In work we have a slogan "Just use jQuery and be done with it"

Code: Select all
//import jQuery before this

<script>
$(document).ready(function() {
  $('#demButtons').click(function() {
    alert(Math.random() >= 0.3); // will evaluate to boolean true or false without branching
  });
});
</script>

....
.....
....

<input type="submit" id="demButtons" value="Submit" />


Untested but you should get the gist
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 653
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Javascript Button

Post by TheBon on Thu Dec 16, 2010 9:31 pm
([msg=50546]see Re: Javascript Button[/msg])

OnlyHuman wrote:The simplest way I can think of is just to encompass that bit of script you already have, within the body of a function, and then call that function from an HTML button input. Something like this maybe?

Code: Select all
<html>

<head>

    <script type="text/javascript" language="javascript">

        function check()
        {
            var result = (Math.random() >= 0.3) ? "True" : "False";
            alert( result );
        }

    </script>

</head>

<body>

    <input type="button" value="Press Me" onclick="check();" />

</body>

</html>

I rewrote your code just a bit. It still does the same thing, just in a different way. There wasn't anything wrong with what you had, I just like to add my own touch to things sometimes.


This did work. Thanks for help :ugeek:


thetan wrote:In work we have a slogan "Just use jQuery and be done with it"<br><br>
Code: Select all
<br>//import jQuery before this<br><br><script><br>$(document).ready(function() {<br>  $('#demButtons').click(function() {<br>    alert(Math.random() >= 0.3); // will evaluate to boolean true or false without branching<br>  });<br>});<br></script><br><br>....<br>.....<br>....<br><br><input type="submit" id="demButtons" value="Submit" /><br>
<br><br>Untested but you should get the gist


I'm just beginning in Javascript. I have no idea what jQuery is. Will look into it when I get a better grasp of JS though.
TheBon
New User
New User
 
Posts: 2
Joined: Thu Dec 16, 2010 2:05 am
Blog: View Blog (0)


Re: Javascript Button

Post by OnlyHuman on Thu Dec 16, 2010 10:27 pm
([msg=50550]see Re: Javascript Button[/msg])

No problem. But, after you get some practice, you should try thetan's way again. His example is actually significantly cleaner than my own. And jQuery is a pretty powerful tool as well. Take your time though. Rome wasn't built in a day right?
OnlyHuman
Poster
Poster
 
Posts: 192
Joined: Sat Aug 22, 2009 1:37 am
Blog: View Blog (0)


Re: Javascript Button

Post by thetan on Fri Dec 17, 2010 11:05 pm
([msg=50637]see Re: Javascript Button[/msg])

No, rome wasn't built in a day.

However, time is running out. For all of us. Every second we live is a second closer to our deaths.

and to got meta on yo asses
"every night i sleep in this bed i get weaker, and for every night charlie squats in a bush he gets stronger" -Apacolypse Now!
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 653
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Javascript Button

Post by kujinR on Fri Jan 07, 2011 4:45 am
([msg=51823]see Re: Javascript Button[/msg])

thetan wrote:In work we have a slogan "Just use jQuery and be done with it"

Truth.

jQuery /is/ JS. Shouldn't be too hard to understand jQuery once you get the basics of JS down.
"Better to keep your mouth shut and be thought a fool than to open it and remove all doubt."

"red = changed"
User avatar
kujinR
Poster
Poster
 
Posts: 268
Joined: Thu Jul 29, 2010 4:39 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests