Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: var code line stopping the rest code from printing hello world

  1. #1
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default var code line stopping the rest code from printing hello world

    hi

    if i comment or remove the line
    Code:
    var pp = document.pform.name.value;
    then the below code prints "hello world" 10 times.

    but if i uncomment that line, then the code prints nothing

    why is that line stopping the code from working.

    no error is seen in firefox console.

    vineet

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    var pp = document.pform.name.value;
    for(i=0;i<10;i++)
    {
    	document.write("Hello World <br>");
    }
    </script>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Two problems. Firstly, that line should be:

    Code:
    var pp = document.getElementById("pform").getAttribute("name");
    ... and secondly the JavaScript should be after the declaration of the element you are looking for so that it is not executed until the document has finished loading. Also it helps debugging if you print out the value of the variable you have recovered.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    var pp = document.getElementById("pform").getAttribute("name");
    for(i=0;i<10;i++)
    {
    	document.write("Hello World "+pp+"<br>");
    }
    </script>
    </body>
    </html>

  3. #3
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi styxlawyer

    i tried to get value with your getElementById method

    but it didnt worked with my function

    please see the below code i have commented the getElementById and used my old syntex line

    there are 2 problems here :

    if i use getElementbyID then i dont get the value and function doesnt print anything.

    secondly the forloop prints "hello world" according to numeric number entered in input field but the browser doesnt stop processing

    try to enter "5" as numeric number in the input field.

    after printing hello world 5 times the browser keeps processing (see screenshot)


    what am i doing wrong ??


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform" onsubmit="myfunc()">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    function myfunc()
    {
    //var pp = document.getElementById("pform").getAttribute("name").value;
    var pp = document.pform.name.value;
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		document.write("Hello World <br>");
    			
    			if(i === pp){ break; }
    		}
    	}
    }	
    </script>
    </body>
    </html>
    here is the screenshot of browser keep processing after forloop stops printing
    Click image for larger version. 

Name:	forloopkeepon.jpg 
Views:	133 
Size:	19.0 KB 
ID:	5944

  4. #4
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    The code I posted was tested locally on my laptop and worked perfectly. You have changed several parts of it, so go back to the origin listing and try again. Furthermore, the variable 'pp' is a string so using it in a test in a 'for' loop is meaningless:

    Code:
    for(i=0;i<=pp;i++)

  5. #5
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi styxlawyer

    i didnt understood

    how is it a string ??

    if a person is entering a number value such as 1 2 3 4 5 ??

    and

    how should i make it a integer

    vineet

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The form hasn't been parsed yet when you try to get a value from it. Put the script after the form or fire it on load or on document ready:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    var pp = document.pform.name.value;
    for(i=0;i<10;i++)
    {
    	document.write("Hello World <br>");
    }
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    The form hasn't been parsed yet when you try to get a value from it. Put the script after the form or fire it on load or on document ready:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    var pp = document.pform.name.value;
    for(i=0;i<10;i++)
    {
    	document.write("Hello World <br>");
    }
    </script>
    </body>
    </html>
    hi john

    my script is already after the form.

    see my post number 3

    now the problem is how to run forloop according to input numeric value

    thanks
    vineet

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    He's right about it being a string. All form values are strings. You can covert it to a number (assuming it's a string version of a number) various ways. The easiest is by giving it a sign:

    Code:
    var pp = +document.pform.name.value;
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		document.write("Hello World <br>");
    			
    			if(i === pp){ break; }
    		}
    	}
    Again, this will only work if it is a number represented as a string (will also work if it really is a number, but as a form value, as I say it is by default a string). Often we test these values first to determine they are not isNaN (is not a number) before proceeding. You might have to go:

    Code:
    var pp = document.pform.name.value;
    pp = +pp;
    Last edited by jscheuer1; 09-13-2016 at 04:51 AM. Reason: correct syntax of isNaN
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi john

    my below code prints "hello world" according to the input number

    if you will enter 4 in input box then "hello world" will be printed 5 times

    but the page doesnt stop processing

    why it keeps on processing even after hello world has been printed 5 times

    see the screenshot
    Click image for larger version. 

Name:	forloopkeepon.jpg 
Views:	118 
Size:	19.0 KB 
ID:	5945

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform" onsubmit="myfunc()">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    function myfunc()
    {
    //var pp = document.getElementById("pform").getAttribute("name").value;
    var pp = +document.pform.name.value;
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		document.write("Hello World <br>");
    			
    			if(i === pp){ break; }
    		}
    	}
    }	
    </script>
    </body>
    </html>

  10. #10
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    OK, so we went up a blind alley because you didn't explain what it was you were trying to do in the first post. I had assumed you were trying to retrieve the name of the form which was incorrect.

    You are seeing an extra "Hello World" because of this line:

    Code:
    for(i=0;i<=pp;i++)
    which should be either:

    Code:
    for(i=0;i<pp;i++)
    or
    Code:
    for(i=1;i<=pp;i++)
    When I run this page on my browser it stops processing after the form has loaded.

Similar Threads

  1. Help with one line of code in Expando
    By porkmeister in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 04-12-2009, 05:41 PM
  2. phpbb3 and the rest of the world
    By Dirt_Diver in forum PHP
    Replies: 3
    Last Post: 02-06-2009, 12:20 PM
  3. Printing only an image, not the rest of the page
    By venu_1200 in forum JavaScript
    Replies: 1
    Last Post: 08-20-2008, 09:52 AM
  4. Printing iframe from DD Tabbed doc code
    By its_jon in forum Dynamic Drive scripts help
    Replies: 5
    Last Post: 04-05-2008, 08:50 AM
  5. Easiest Language [real world, not code]
    By djr33 in forum The lounge
    Replies: 79
    Last Post: 11-26-2007, 04:12 PM

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
  •