SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict betrollwin.com's Avatar
    Join Date
    Nov 2005
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    NaN Instead of Integer

    Code JavaScript:
    <html>
    <head>
    	<script type="text/javascript">
    	var x = parseInt(document.getElementById("startrow").value);
    	function generateRowg() {
    		x += 1; 
    		var c=document.getElementById("showrowsg");
    		c.innerHTML+="yaychange" + x + "<br><br>";
    	}
    	</script>
    </head>
    <body>
    <input type="hidden" id="startrow" value="3">
    <p><a style="color:blue" onclick="generateRowg()">Add Row</a></p>
    <span id="showrowsg"></span>
    </body>
    </html>

    I see NaN instead of a number each time. Any help?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    x needs to be assigned after the page has fully loaded or more specifically the input element. Alternatively the script could moved to the very bottom just before the closing HTML tag.
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your code wouldn't work since your trying to grab an element value that to the page doesn't exist yet. Try the below code

    Code JavaScript:
    var x;
     
    window.onload = function() {
        x = parseInt(document.getElementById("startrow").value);
    }
     
    function generateRowg() {
        x += 1;
        var c=document.getElementById("showrowsg");
        c.innerHTML+="yaychange" + x + "<br><br>";
    }

  4. #4
    SitePoint Addict betrollwin.com's Avatar
    Join Date
    Nov 2005
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both. Sgt's code works flawlessly.


Bookmarks

Posting Permissions

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