0

Another rookie-question, which is rooted in a very shallow understanding of JS.

I'm building a small online multistep-form (made in a single html by hiding divs), where every button push is increasing a var with a fixed number:

JS:

var score = 20;

function updateScore (){
            score = score + 10;
            result.value = score;
        }

I wish to display the score:

Using <input type="text" name="result" id="result" value="" size="" readonly> I get the correct value (30).

However when I use <script>document.write(result)</script> I get nothing and using <script>document.write(score)</script> I get the original var (20).

Same problem when I want the var to output in a jquery progress-bar.

How can I make sure that it is the updated var, which displays in document.write?

EDIT: Complete JS

var currentLayer = 'lp';
var score = 10;

function showLayer(lyr){
    hideLayer(currentLayer);
    document.getElementById(lyr).style.visibility = 'visible';
    document.getElementById(lyr).style.display = 'block';
    currentLayer = lyr;
}
function hideLayer(lyr){
    document.getElementById(lyr).style.visibility = 'hidden';
    document.getElementById(lyr).style.display = 'none'
}

function goTo(lyr){
    showLayer(lyr);
    score = score + 10;
    resultat.value = score;
}
function goToMinus(lyr){
    showLayer(lyr);
    score = score - 10;
    resultat.value = score;
}    

  $(function() {
    $( "#progressbar" ).progressbar({
      value: score
      });
  });
dblaursen
  • 27
  • 1
  • 7

2 Answers2

0

result is a HTML Input Element, so printing the result would be:

<script>document.write(result.value)</script>

About writting the right "score" var vlaue ensure that there's only one "var score" and it's in the same scope.

Edorka
  • 1,781
  • 12
  • 24
0

I would suggest using document.getElementById('result').value opposed to result.value within your updateScore function to ensure your accessing the correct element.

I had included an example where setInterval will call your updateScore every second to illustrate the result being updated,

var score = 20;

function updateScore (){
            score = score + 10;      
            document.getElementById("result").value = score;
        }

setInterval(updateScore,1000);

This can be modified at the following fiddle,

http://jsfiddle.net/d3QqN/

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115