4

Im trying to create a small memory game... in which the time is multiplied by the number of moves made by the user...

After the user finishing making all pars, the javascript runs function:

 function finish() {
    stopCount();
    var cnt1 = $("#counting").val();
    var tim = $("#timecount").val();
    var totalpuntos = cnt1 * tim; 
    var mensaje = "Congrats! You made <strong>"+cnt1+"</strong> moves in <strong>"+tim+"</strong> seconds, making a total of <strong>"+totalpuntos+"</strong> points!";
    $('#finaldiv').show();
}

In the HTML goes like this:

<div id="finaldiv">
<div style="width: 100%; height: 100%; background-image: url('images/overall.png'); z-index:990; position: absolute; top: 0; left: 0;">
<div style="background-color:#fff; margin:100px auto 0 auto; height:300px; width:500px; padding:10px">

<script language="javascript">
document.write (mensaje);
</script>

</div>
</div>
</div>

It does show the text box but it doesn't display the message :(

It works if I add this to the function:

 alert(mensaje);

But I need it to be displayed in the box and not in a alert message, so the score can be submitted...

What is wrong with the script? why it doesn't display the message in the box? :(

1
  • remove var from where you assign a value to mensaje so that it becomes a global variable. Currently, it only exists inside the function. Commented Feb 4, 2014 at 10:05

4 Answers 4

6

I hope you want to show the score within the final div. Why don't you just fill it before you show the div.

function finish() {
    stopCount();
    var cnt1 = $("#counting").val();
    var tim = $("#timecount").val();
    var totalpuntos = cnt1 * tim; 
    var message = "Congrats! You made <strong>"+cnt1+"</strong> moves in <strong>"+tim+"</strong> seconds, making a total of <strong>"+totalpuntos+"</strong> points!";
    $('#message').html(message);
    $('#finaldiv').show();
}

EDIT: Modify your HTML like this.

<div id="finaldiv">
    <div style="width: 100%; height: 100%; background-image: url('images/overall.png'); z-index:990; position: absolute; top: 0; left: 0;">
        <div id="message" style="background-color:#fff; margin:100px auto 0 auto; height:300px; width:500px; padding:10px">
    </div>
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

Finally someone that fixes the real issue in this question. One suggestion: Chain .append() and .show(), and select the proper element like this: $('#finaldiv div div').append(mensaje).show();
@Cerbrus I added the chaining as you specified. For the selector i am not sure if that's the correct approach. Thank you for the improvement.
A better option for the selector would be to add an id to the inner element.
@Cerbrus What do you think about the answer now. I made the changes you specified.
Excellent! That's the way to do it :)
2

It's partly because of scoping. The mensaje variable is only accessible from that function, not the rest of it. You can either define it in the global scope (var mensaje; at the beginning of your code), or have your finish() function return the value.

The rest of the problem is that you are writing to the document before the function executes.

6 Comments

That's not the issue. document.write() is being executed on page load. The function's probably running after that.
It's certainly part of the issue.
No, the div's content shouldn't even be modified outside of that function. He should just set the message from the function itself. Scope's not the issue, it's the way the OP tries to set the content.
I agree that the OP is doing it incorrectly, but this is at the very least a start.
You're not fixing the issue here. This will not work, because finish() will most likely run after the write. The main issue is the way the user tries to handle the output
|
1

Since you're using jQuery, you can use it's $.html() method:

$(' YOUR TARGET ELEMENT ').html(mensaje);

A live example at JSBin

This, will replace all content of your target element. If there's content you need to keep in that element, just use $.append() instead of $.html().

Documentation:

Hope it helps.

Comments

0

The issue is that you're trying to set the div's content at a place where your code has no idea weather or not the finish function is running.

Replace your script with this:

function finish() {
    stopCount();
    var cnt1 = $("#counting").val();
    var tim = $("#timecount").val();
    var totalpuntos = cnt1 * tim; 
    var mensaje = "<div>Congrats! You made <strong>"+cnt1+"</strong> moves in <strong>"+tim+"</strong> seconds, making a total of <strong>"+totalpuntos+"</strong> points!</div>";
    $('#message').append(mensaje);
    $('#finaldiv').show();
}

And your HTML with this:

<div id="finaldiv">
    <div style="width: 100%; height: 100%; background-image: url('images/overall.png'); z-index:990; position: absolute; top: 0; left: 0;">
        <div id="message" style="background-color:#fff; margin:100px auto 0 auto; height:300px; width:500px; padding:10px">
        </div>
    </div>
</div>

This adds a id to the inner div, to make it easier to access. It also removes the document.write(). That function was not how this should (or could) be handled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.