3 Replies - 678 Views - Last Post: 24 October 2013 - 04:29 AM

#1 ronanbrowne88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-November 12

keeping button onclick

Posted 24 October 2013 - 04:03 AM

below is a basic program i wrote to allow a user to enter a number and compare it to a preseleted number and tell the user if they guessed right.

I want to change it so that when i click the button the button does not disappear,the button remains, then depending on your text the apropriate string of text apears beside the button then you click to geuss again.

firstly i have no idea how to keep the button after some one clicks it cant seem to find any thing about that online, do i use something along the lines of .innerHTML ? anyway if someone could point me in right direction would be great!



	<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<link type="text/css" rel="stylesheet" href="main.css" />
	

	<title>lucky numbers </title>
	
	<button type="button" onclick="myFunction()">Click Me!</button>
	
	<script type = "text/javascript">
	
	
	var winningNum = 8;
	
	function myFunction()
{

	
		
		var geuss = "false";
		var num = prompt("please enter a number","x");
		
		while(geuss=="false"){
		
		
			if(num==winningNum){
			document.write("congrats you got the right number</br>");
			geuss="true";
			
			}
			else {
			document.write("Sorry try again</br>");
			var num = prompt("please enter a number","x");
			}
		
		
		}
		
		
}




Is This A Good Question/Topic? 0
  • +

Replies To: keeping button onclick

#2 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 199
  • Joined: 19-October 13

Re: keeping button onclick

Posted 24 October 2013 - 04:18 AM

When you call document.write you are clearing the whole page, not just the button.

To do what you want to do you can create an element where you want the text to appear, and give it an ID so that you can easily refer to it from your script.
<span id="response"></span>

Use getElementById to access the element from your script and set innerHTML to the text that you want to show.
document.getElementById('response').innerHTML = 'text';

Was This Post Helpful? 1
  • +
  • -

#3 ronanbrowne88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-November 12

Re: keeping button onclick

Posted 24 October 2013 - 04:24 AM

Thanks , works perfectly !
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: keeping button onclick

Posted 24 October 2013 - 04:29 AM

Quote

I want to change it so that when i click the button the button does not disappear

normally, a button does not disappear when you click it.

in your case it disappears for a reason many beginners encounter: document.write(). it simply kills your script (because you canít write on a closed document and therefore document.write() opens a new one and therefore completely discarting the current).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1