By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,588 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,588 IT Pros & Developers. It's quick & easy.

Displaying Javascript variable in text box.

P: n/a
Hi.

I'm creating a simple memory game in which the user has to make pairs
from cards. I would like the user to see the variable "trys" (the
score) update on the page dynamically (in a text box would be great).
I can do this by showing a msgbox every time try increments but this
is annoying to use. I have been trying for hours to get this to work,
using innerhtml.

function turnOver(e)
{
if (matchCount<8 & !matched[e.name]) //If the game hasn't finished
and the chosen card isn't already matched then...
{

if (clicked<1) //If this is the first card
{
firstCard=e; //grab first card object
pair=cards[firstCard.name]; //get first pair number from cards
array
e.src=document.images["load"+pair].src; //flip over first card
}

else //If one card is already chosen
{
e.src=document.images["load"+cards[e.name]].src; //flip over second
card

if (pair!==cards[e.name]) //If cards aren't the same then...
{
trys++ //UPDATE THE SCORE ON THE PAGE HERE
alert("No Match! " + trys + " trys so far")
firstCard.src=document.images["load0"].src; //flip first card back
e.src=document.images["load0"].src; //flip second card back
}

else //If cards are the same then...
{
//Record matched cards and increase matchCount
matched[firstCard.name]=true;
matched[e.name]=true;
matchCount++;
trys++ //UPDATE THE SCORE ON THE PAGE HERE
}
}

Any help would be greatly appreciated.

Mike

Feb 1 '07 #1
Share this Question
Share on Google+
3 Replies

P: n/a
dd
On Feb 1, 8:42 pm, mbayn...@gmail.com wrote:
I have been trying for hours to get this to work,
using innerhtml.
Can you show the code that tried to do innerHTML?
I wonder whether you did all lower case innerhtml
instead of innerHTML. This is an example of writing
to innerHTML:

Current Tries: <div id="tries">0</div>

<script language="javascript">
function updateTries(n){
var o=document.getElementById("tries");
if(o){
o.innerHTML=n;
}
}

Feb 1 '07 #2

P: n/a
Hey. Thanks for your quick reply. The inner"html" was just a typo. I
wish that was my mistake. I have been looking at examples on w3
schools but they don't seem to have anything similar.
All I want to do is display the variable "trys" on the page and when
"trys"updates so too does the text on the page. It sounds easy but
has taken up most of my day!

Thanks

Mike
Feb 1 '07 #3

P: n/a
Thanks for the help. It was much easier than I thought

"document.scoreForm.scoreBox.value = trys"

Mike

Feb 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.