SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)

    Help on very simple script

    I have no clue with java. I found this on the net and was trying to manipulate it for what I want but was unsuccessful.

    Code:
    <html>
    <head>
    <title>Our Products</title>
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <style type="text/css"> 
    	.submenu { padding-left:15px;}
    		</style>
    <script type="text/javascript">
    <!--
    
    
    
    function addItem(cb){
    
    if(document.getElementById){
    var elAmt = document.getElementById('Amt');
    var elExtra = document.getElementById('extraAmt');
    var elSub = document.getElementById('subAmt');
    var elTax = document.getElementById('taxAmt');
    var elTotal = document.getElementById('totAmt');
    		var amount = parseFloat(elAmt.innerHTML);
    		if(cb.checked){
    		elExtra.innerHTML = "4.99";
    		elSub.innerHTML = amount + 4.99;
    		elTax.innerHTML = (amount + 4.99) * .06;
    		elTotal.innerHTML = ((amount + 4.99) * 1.06);
    		}
    		else{
    		elExtra.innerHTML = "0.00"
    		elSub.innerHTML = "10.00"
    		elTax.innerHTML = "0.60"
    		elTotal.innerHTML = "10.60";
    		}
    	}
    }
    
    
    -->
    </script>
    </head>
    <body>
    <p><b>ProductList</b></p>
    
    <br>
    <br>
    Product Information:
    
    <table>
    <tr><td>Item Description</td><td id='Amt'>10.00</td></tr>
    <tr><td><input type=checkbox name=mType onclick="addItem(this)" value="prod3">Add extra product for 4.99</td><td id='extraAmt'>0.00</td></tr>
    <tr><td>subtotal</td><td id='subAmt'>10.00</td></tr>
    <tr><td>sales tax</td><td id='taxAmt'>0.60</td></tr>
    <tr><td>grand total</td><td id='totAmt'>10.60</td></tr>
    </table></form>
    </body></html>
    What I need is 3 or 4 different inputs (input a number into a text box) in which it will add all together (along with a few other operations) and then output it somehow. I could figure out the other part for myself if someone could help with with the main part of the code and form. Thank you in advance!
    Last edited by K. Wolfe; Nov 15, 2007 at 14:28.

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To retrieve a string from a text box use its value property.
    Code:
    var val = parseFloat(document.getElementById("txtVal").value);

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    thanks!

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    I'm still a little lost on this. I understand what I am supposed to do, but I am having trouble rewriting it.

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a more detailed example:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Text Box Example</title>
    
    <script type="text/javascript">
    function calculate() {
    	var val1 = parseFloat(document.getElementById("txt1").value);
    	var val2 = parseFloat(document.getElementById("txt2").value);
    	
    	var newVal = val1 + val2;
    	
    	var paraAnswer = document.getElementById("answer");
    	paraAnswer.innerHTML = newVal;
    }
    </script>
    
    </head>
    
    <body>
    <label for="txt1">Value 1:</label>
    <input type="text" id="txt1" /><br />
    <label for="txt2">Value 2:</label>
    <input type="text" id="txt2" /><br />
    <input type="button" id="btnCalc" value="Calculate" onclick="calculate();"  />
    <hr />
    <p id="answer"></p>
    
    </body>
    </html>

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    Have one more question:

    Code:
    <script type="text/javascript">
    function calculate() {
    	var val1 = parseFloat(document.getElementById("mycredits").value);
    	var val2 = parseFloat(document.getElementById("mybytes").value);
    	var val3 = parseFloat(document.getElementById("mymarks").value);
    	var val4 = parseFloat(document.getElementById("myexp").value);
    	var val5 = parseFloat(document.getElementById("myarena").value);
    	var val6 = parseFloat(document.getElementById("myclearing").value);
    	var val7 = parseFloat(document.getElementById("myother").value);
    	
    	var nval1 = val1 / 500000;
    	var nval2 = val2 * .4;
    	var nval3 = val3 * .002;
    	var nval4 = val4 / 1000000;
    	var nval5 = val5 / 100;
    	var nval6 = val6 / 50;
    	var nval7 = val7 / 10000;
    		
    	var totalGP = nval1 + nval2 + nval3 + nval4 + nval5 + nval6 + nval7;
    	
    	var paraAnswer = document.getElementById("answer");
    	paraAnswer.innerHTML = totalGP;
    	
    	var difference = totalGP;
    	
    	if (difference < 1) {
    	form.comment.value="Rank 1";
    	}
    	if (difference > 2 && difference < 10) {
    	form.comment.value="Rank 2";
    	}
    	if (difference > 10 && difference < 30) {
    	form.comment.value="Rank 3";
    	}
    	if (difference > 31 && difference < 50) {
    	form.comment.value="Rank 4";
    	}
    	if (difference > 51 && difference < 80) {
    	form.comment.value="Rank 5";
    	}
    	if (difference > 80) {
    	form.comment.value="Rank 6"; 
    	}
    	
    }
    </script>
    How would I rewrite the section in bold so that it will display another <p> field with the output based on totalGP?


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
  •