Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    60
    Thanks
    6
    Thanked 6 Times in 6 Posts

    Question Variable Problems

    I have the following code...
    Code:
    <center>Shoot an "A"</center>
    <div class="shoot"><span onclick="alert('Miss');">BTEHJD</span><span id="a" onclick="document.all['a'].innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">GDHJD</span><span id="b" onclick="document.all['b'].innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">KBFKBM<br>B</span><span id="c" onclick="document.all['c'].innerHTML = ' ';score();">A</span><span id="d" onclick="document.all['d'].innerHTML = ' ';score();">A</span><span id="e" onclick="document.all['e'].innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">BGDJJVB</span><span id="f" onclick="document.all['f'].innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">G</span><span id="g" onclick="document.all['g'].innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">HDJNJJ<br>HDFGBJVBKJGVJG</span><span id="h" onclick="document.all['h'].innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">HFGTR</span></div>
    <style>.shoot{cursor: crosshair; height: 75px; width: 200px; BORDER: solid 2px grey;}</style>
    <button onclick="reset()">Reset</button>Score:<span id="score">0</span>
    <script>
    var score=0
    function reset(){
    document.all['a'].innerHTML = 'A';
    document.all['b'].innerHTML = 'A';
    document.all['c'].innerHTML = 'A';
    document.all['d'].innerHTML = 'A';
    document.all['e'].innerHTML = 'A';
    document.all['f'].innerHTML = 'A';
    document.all['g'].innerHTML = 'A';
    document.all['h'].innerHTML = 'A';
    var score=0;
    document.all['score'].innerHTML = '0';
    }
    function score(){
    var score=score+1;
    document.all['score'].innerHTML = score;
    }
    </SCRIPT>
    But the score always says zero can anyone
    tell me what I am doing wrong?

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Don't use document.all is deprecated and is IE propietary, use document.getElementById() instead.

    Secondly, avoid using the same variable name as to your function name.

    You might find this modification useful:
    Code:
    <center>Shoot an "A"</center>
    <div class="shoot"><span onclick="alert('Miss');">BTEHJD</span><span id="a" onclick="document.getElementById('a').innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">GDHJD</span><span id="b" onclick="document.getElementById('b').innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">KBFKBM<br>B</span><span id="c" onclick="document.getElementById('c').innerHTML = ' ';score();">A</span><span id="d" onclick="document.getElementById('d').innerHTML = ' ';score();">A</span><span id="e" onclick="document.getElementById('e').innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">BGDJJVB</span><span id="f" onclick="document.getElementById('f').innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">G</span><span id="g" onclick="document.getElementById('g').innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">HDJNJJ<br>HDFGBJVBKJGVJG</span><span id="h" onclick="document.getElementById('h').innerHTML = ' ';score();">A</span><span onclick="alert('Miss');">HFGTR</span></div>
    <style>.shoot{cursor: crosshair; height: 75px; width: 200px; BORDER: solid 2px grey;}</style>
    <button onclick="reset()">Reset</button>Score:<span id="score">0</span>
    <script>
    var score1=0;
    function reset(){
    document.getElementById('a').innerHTML = 'A';
    document.getElementById('b').innerHTML = 'A';
    document.getElementById('c').innerHTML = 'A';
    document.getElementById('d').innerHTML = 'A';
    document.getElementById('e').innerHTML = 'A';
    document.getElementById('f').innerHTML = 'A';
    document.getElementById('g').innerHTML = 'A';
    document.getElementById('h').innerHTML = 'A';
    score1=0;
    document.getElementById('score').innerHTML = '0';
    }
    function score(){
    score1+=1;
    document.getElementById('score').innerHTML = score1;
    }
    </SCRIPT>
    Lastly, center is a deprecated element. Find sometime to fix it.

    Hope it helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    60
    Thanks
    6
    Thanked 6 Times in 6 Posts
    Now I did what you recomended and added some more and have this code...
    Code:
    <center>Shoot an "A"</center>
    <div class="shoot"><span onclick="scoredown();">BTEHJD</span><span id="a" 
    
    onclick="this.style.visibility = 'hidden';score();">A</span><span onclick="scoredown
    
    ();">GDHJD</span><span id="b" onclick="this.style.visibility = 'hidden';score
    
    ();">A</span><span onclick="scoredown();">KBFKBM<br>B</span><span id="c" 
    
    onclick="this.style.visibility = 'hidden';score();">A</span><span id="d" 
    
    onclick="this.style.visibility = 'hidden';score();">A</span><span id="e" 
    
    onclick="this.style.visibility = 'hidden';score();">A</span><span onclick="scoredown
    
    ();">BGDJJVB</span><span id="f" onclick="this.style.visibility = 'hidden';score
    
    ();">A</span><span onclick="scoredown();">G</span><span id="g" 
    
    onclick="this.style.visibility = 'hidden';score();">A</span><span onclick="scoredown
    
    ();">HDJNJJ<br>HDFGBJVBKJGVJG</span><span id="h" onclick="this.style.visibility = 
    
    'hidden';score();">A</span><span onclick="scoredown();">HFGTR</span></div>
    <style>.shoot{cursor: crosshair; height: 75px; width: 200px; BORDER: solid 2px grey;}
    
    </style>
    <button onclick="reset()">Reset</button><button onclick="show()">Show Missed 
    
    A's</button>Score:<span id="score">0</span>
    <script>
    var score1=0;
    function reset(){
    document.getElementById('a').style.visibility = 'visible';
    document.getElementById('b').style.visibility = 'visible';
    document.getElementById('c').style.visibility = 'visible';
    document.getElementById('d').style.visibility = 'visible';
    document.getElementById('e').style.visibility = 'visible';
    document.getElementById('f').style.visibility = 'visible';
    document.getElementById('g').style.visibility = 'visible';
    document.getElementById('h').style.visibility = 'visible';
    document.getElementById('a').style.color = 'black';
    document.getElementById('b').style.color = 'black';
    document.getElementById('c').style.color = 'black';
    document.getElementById('d').style.color = 'black';
    document.getElementById('e').style.color = 'black';
    document.getElementById('f').style.color = 'black';
    document.getElementById('g').style.color = 'black';
    document.getElementById('h').style.color = 'black';
    var score1=0;
    document.getElementById('score').innerHTML = score1;
    }
    function score(){
    score1+=1;
    document.getElementById('score').innerHTML = score1;
    }
    function scoredown(){
    score1-=1;
    document.getElementById('score').innerHTML = score1;
    }
    function show(){
    document.getElementById('a').style.color = 'red';
    document.getElementById('b').style.color = 'red';
    document.getElementById('c').style.color = 'red';
    document.getElementById('d').style.color = 'red';
    document.getElementById('e').style.color = 'red';
    document.getElementById('f').style.color = 'red';
    document.getElementById('g').style.color = 'red';
    document.getElementById('h').style.color = 'red';
    }
    
    </SCRIPT>
    But even when you press reset the next time you click an "A" it goes back to your old scoreHere is an example...Click Here

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Change
    var score1=0;
    document.getElementById('score').innerHTML = score1;
    to
    Code:
    score1=0;
    document.getElementById('score').innerHTML = score1;
    (when you add var there, it just creates a new local variable, leaving the global variable's value intact)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •