Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2012, 02:30 PM   PM User | #1
cmyk529
New to the CF scene

 
Join Date: Oct 2012
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
cmyk529 is an unknown quantity at this point
Question NEWBIE - Random text generator problem, button not staying?

Hiya, this is my first post - I'll try to make sure I'm doing things correct!

I'm trying to create a random text generator, I've got the random text generating part down, but after I click the button and get the random text, I lose my button so I can't continue to generate random text. Help?

Code:
<!DOCTYPE html>
<html>
<body>

<p id="generator">Generate a random fact</p>

<button onclick="myFunction()">Try it</button>

<script>
Array.prototype.myUcase=function()
{
for (i=0;i<this.length;i++)
  {
  this[i]=this[i].toUpperCase();
  }
}

function myFunction()
{
var r_text = new Array ();
r_text[0] = "Give a homeless man a gift card";
r_text[1] = "Hold the door for a stranger";
r_text[2] = "Pay for a stranger's lunch";
r_text[3] = "Compliment the next person you see";
r_text[4] = "Take your neighbor's dog for a walk";
r_text[5] = "Send greeting cards to random addresses";
r_text[6] = "Load a vending machine with money";
var i = Math.floor(7*Math.random())

document.write(r_text[i]);
}

</script>

</body>
</html>

<script language="JavaScript">
<!--

//-->
</script>
Here is a snippit if that doesn't work:

http://snipt.org/vfAe1


Also, any resources or directions you can provide would be great, my knowledge is good in some areas, and I'm a complete idiot in other areas!
cmyk529 is offline   Reply With Quote
Old 10-09-2012, 02:35 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Code:
document.write(r_text[i]);
You cannot use a document.write after the page has been rendered.
If you call it, it reloads the page to the original contents.

Consider creating a <div> area with an ID and using .innerHTML = r_text[i] instead
jmrker is offline   Reply With Quote
Old 10-09-2012, 03:00 PM   PM User | #3
cmyk529
New to the CF scene

 
Join Date: Oct 2012
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
cmyk529 is an unknown quantity at this point
Code:
<!DOCTYPE html>
<html>
<body>
    

<p id="generator">Generate a random fact</p>

<button onclick="myFunction()">Try it</button>

<div id="content">
   <script type="text/javascript">
    function changeText(){
	document.getElementById('boldStuff').innerHTML = r_text[i];
}
</div>

<div id="random">   
<script>
Array.prototype.myUcase=function()
{
for (i=0;i<this.length;i++)
  {
  this[i]=this[i].toUpperCase();
  }
}

function myFunction()
{
var r_text = new Array ();
r_text[0] = "Give a homeless man a gift card";
r_text[1] = "Hold the door for a stranger";
r_text[2] = "Pay for a stranger's lunch";
r_text[3] = "Compliment the next person you see";
r_text[4] = "Take your neighbor's dog for a walk";
r_text[5] = "Send greeting cards to random addresses";
r_text[6] = "Load a vending machine with money";
var i = Math.floor(7*Math.random())

document.getElementById('boldStuff').innerHTML = r_text[i];
}

</script>
</div>

</body>
</html>

<script language="JavaScript">
<!--

//-->
</script>

Not sure if I've done this correctly.
cmyk529 is offline   Reply With Quote
Old 10-09-2012, 03:27 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<!DOCTYPE html>
<head>
</head>
<body>
    
<p id="generator">Generate a random fact</p>

<button onclick="myFunction()">Try it</button>

<div id="boldStuff"">
</div>

<script type = "text/javascript">

function myFunction() {
var r_text = [];
r_text[0] = "Give a homeless man a gift card";
r_text[1] = "Hold the door for a stranger";
r_text[2] = "Pay for a stranger's lunch";
r_text[3] = "Compliment the next person you see";
r_text[4] = "Take your neighbor's dog for a walk";
r_text[5] = "Send greeting cards to random addresses";
r_text[6] = "Load a vending machine with money";
var i = Math.floor(7*Math.random())

document.getElementById('boldStuff').innerHTML = r_text[i].toUpperCase();
}

</script>
</div>

</body>
</html>
A small defect is that the same quote may appear twice in succession by random selection. You can overcome this by shuffling your array.

Your uppercase function is quite superflous here.

<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.


Quizmaster: Which African country gives its name to the complaint known as 'gippy tummy'?
Contestant: Venezuela.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
cmyk529 (10-18-2012)
Reply

Bookmarks

Tags
button, java, random, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 PM.


Advertisement
Log in to turn off these ads.