Ajax Text Box Help!

Discussion in 'Programming' started by bbooker01, May 19, 2011.

  1. #1
    Alright so I was wondering how I can make a text box which will have a quotation in, and under that is a button and it says tap me and each time you click it the quote changes in the text box. How can I accomplish this? Thanks!
     
    bbooker01, May 19, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How about this?
    
    <html>
    <head>
    <script type="text/javascript">
    	var curidx;
    	var quotes = new Array();
    	quotes[0] = 'This is quote 1';
    	quotes[1] = 'This is quote 2';
    	quotes[2] = 'This is quote 3';
    	quotes[3] = 'This is quote 4';
    	quotes[4] = 'This is quote 5';
    	
    	window.onload = function() {
    		curidx = Math.floor(Math.random()*5);
    		changeQuote();
    	}
    	
    	function changeQuote() {
    		do {
    			var idx = Math.floor(Math.random()*5);
    		} while (idx == curidx);
    		document.getElementById('quote').innerHTML = quotes[idx];
    		curidx = idx;
    	}
    </script>
    <style type='text/css'>	
    * {
    	margin: 0;
    	padding: 0;
    }
    
    #quotebox {
    	border: 1px solid #000;
    	line-height: 25px;
    	margin: 10px;
    	padding: 10px;
    	text-align: center;
    	width: 180px;
    }
    
    #quotebtn {
    	height: 26px;
    	width: 80px;
    }
    </style>
    </head>
    <body>
    <div id='quotebox'>
    	<p id='quote'></p>
    	<button id='quotebtn' onclick='changeQuote()'>Tap Me</button>
    </div>
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, May 19, 2011 IP
  3. Sefrez

    Sefrez Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This will do basically what he wants, though he may want to have a database server side (or the like) that AJAX would be required to deal with.
     
    Sefrez, May 21, 2011 IP