Why doesn't this script increment correctly!

Discussion in 'JavaScript' started by sin0cide, Nov 7, 2007.

  1. #1
    I have tried may ways and it just starts at 0 then adds 1 and stays at one no matter how many cells are there what am I doing wrong?

    <html>
    <head>
    <script type="text/javascript">
    function insCell() {
    	var apples = 0
    	if (apples<5) {
      	  apples ++
    	  var x=document.getElementById('tr1').insertCell(0)
    	  x.innerHTML="John"
    	  alert("Apples value: " + apples)
    	}
    }
    </script>
    </head>
    <body>
    
    <table border="1">
    <tr id="tr1">
    <td>Peter</td>
    </tr>
    </table>
    <br />
    <input type="button" onclick="insCell()" value="Insert cell">
    
    </body>
    </html>
    
    Code (markup):

     
    sin0cide, Nov 7, 2007 IP
  2. ezpz

    ezpz Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You're setting apples=0 inside the function - you need to move it outside.
     
    ezpz, Nov 7, 2007 IP
  3. sin0cide

    sin0cide Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yah I figure that out the hard way... thanks for the input... I have been looking at the replies on this topic and always saw 0 but when I click on the topic I noticed you replied wtf.
     
    sin0cide, Nov 7, 2007 IP
  4. ezpz

    ezpz Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Glad you figured it out. Coincidentally I changed my signature yesterday so that it doesn't contain a link - though I don't know why that would stop my replies showing on the forum display. If it keeps on happening I'll get in touch with DigitalPoint.
     
    ezpz, Nov 7, 2007 IP