www.webdeveloper.com
Results 1 to 4 of 4

Thread: Alternative for document.write ?

  1. #1
    Join Date
    Jan 2011
    Posts
    2

    Alternative for document.write ?

    I have a simple calculator here that takes the number of songs from the input text box and multiplies it by 500 and then tells you the total. I am doing this on a simple web page for a project for college. I know document.write overwrites my entire page to print the result but I would like to keep the page and design that I have made and to print the result on the same paragraph as where the script and text box are.


    Below is my javascript:

    Code:
    <script type="text/javascript">
    
    function kalkulo()
    {
    var x;
    x = *document.form1.inputbox.value;
    var y;
    y=500;
    var numrix, numriy;
    numrix=parseInt(x);
    numriy=parseInt(y);
    res= numrix*numriy
    document.writeln("The price of "+x+" songs is "+res+" euros");
    
    }
    </script>

  2. #2
    Join Date
    Aug 2009
    Posts
    593
    You didn't supply your <form> so this is a guess...

    HTML 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" xml:lang="en" lang="en">
    	<head>
    		<title>document.write alternative</title>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    		<script type="text/javascript">
    			function kalkulo()
    			{
    				var
    					x = document.form1.inputbox.value,
    					y = 500,
    					numrix, numriy,
    					res;
    					
    				numrix = parseInt(0 + x, 10);
    				numriy = parseInt(y, 10);
    				res = numrix * numriy;
    				document.getElementById('result').innerHTML = "The price of " + numrix + " songs is " + res + " euros";
    			}
    		</script>
    
    	</head>
    	<body>
    
    		<form name="form1">
    			<input type="text" name="inputbox" onkeyup="kalkulo();" />
    			<div id="result"></div>
    		</form>
    
    	</body>
    </html>

  3. #3
    Join Date
    Jan 2011
    Posts
    2
    thnx for the effort m8

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by ghforces View Post
    I know document.write overwrites my entire page to print the result but I would like to keep the page and design that I have made and to print the result on the same paragraph as where the script and text box are.
    In general you normally assign an id to a container on your page where you want the output to appear and then assign the output to the container's innerHTML, value or whatever is appropriate property.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles