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 07-19-2011, 02:38 PM   PM User | #1
VeryBasic
New to the CF scene

 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
VeryBasic is an unknown quantity at this point
Unhappy Random Quote Generator Script Help (For Uni Project)

I have to create a website as a project for my university course. Said course has nothing at all to do with computers really so it's really throwing us into the deep end as the majority of us have/had no interest in this particular field.

Regardless, I am creating a website that uses the Random Quote Generator Javascript that I copied from the Hotscripts website. The code details are below and my question is: is there any way I could change the formatting for each quote? For example, change quote 1 to be, say, yellow or bold and change quote 3 to be aligned right and blue? I mainly want to change the colour for each quote but having any other info on how to format it would be really helpful.

Code:
var quotes=new Array();
quotes[0] = "This is quote 1.";
quotes[1] = "This is quote 2.";
quotes[2] = "This is quote 3.";

var q = quotes.length;
var whichquote=Math.round(Math.random()*(q-1));
function showquote(){document.write(quotes[whichquote]);}
showquote();
Also if you could put it into total idiot-speak for me I'd be oh so grateful.
VeryBasic is offline   Reply With Quote
Old 07-19-2011, 03:41 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Lightbulb Something to try...

Try this ...
Code:
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>

<style type="text/css">
/* use whatever CSS control combinations you desire for display */
em { color:hotpink; }
.quot0 { text-align:left; background-Color:cyan; color:black; }
.quot1 { text-align:right; background-Color:yellow; color:blue; }
.quot2 { text-align:center; background-Color:orange; color:black; }
.quot3 { text-align:left; background-Color:black; color:white; }
.quot4 { text-align:right; background-Color:red; color:white; }
.quot5 { text-align:center; background-Color:lime; color:black; }
.quot6 { text-align:left; background-Color:white; color:black; }
</style>

<script type="text/javascript">
var quotes = [
    "This is <em>quote 1.</em>",
    "This is <em>quote 2.</em>",
    "This is <em>quote 3.</em>",
    "This is <em>quote 4.</em>",
    "This is <em>quote 5.</em>",
    "This is <em>quote 6.</em>",
    "This is <em>quote 7.</em>"  // No comma after last entry
];
var qarray = ['quot0','quot1','quot2','quot3','quot4','quot5','quot6'];

function rndQuote() {
  var whichQuote=Math.floor(Math.random()*(quotes.length));
  document.getElementById('Quote').innerHTML = quotes[whichQuote]; 
  document.getElementById('Quote').className = qarray[whichQuote];
/*  for testing purposes only
  alert(document.getElementById('Quote').className+'\nColor: '+
        document.getElementById('Quote').style.color+'\nBGcolor: '+
        document.getElementById('Quote').style.backgroundColor
  );
*/
}

window.onload = function() { rndQuote(); }
</script>

</head>
<body>
<div id="Quote" class="quot0"></div>

<!-- next line is optional for testing -->
<p><button onclick="rndQuote()">Test Quote</button>
</body>
</html>
Add to or subtract from the CSS class styles to fit your quote needs.
jmrker is offline   Reply With Quote
Old 07-19-2011, 04:23 PM   PM User | #3
VeryBasic
New to the CF scene

 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
VeryBasic is an unknown quantity at this point
That appears to work perfectly. Thank you so much for your help, I was getting majorly stressed out over it. You've made my day, man.
VeryBasic is offline   Reply With Quote
Old 07-19-2011, 08:36 PM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Thumbs up

Quote:
Originally Posted by VeryBasic View Post
That appears to work perfectly. Thank you so much for your help, I was getting majorly stressed out over it. You've made my day, man.
You are most welcome.
Happy to help.

Note: there are a number of display conditions you can control with the CSS statements.

Good Luck!
jmrker is offline   Reply With Quote
Reply

Bookmarks

Tags
formatting, generator, javascript, random

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 03:09 AM.


Advertisement
Log in to turn off these ads.