www.webdeveloper.com
Results 1 to 2 of 2

Thread: Function - how to use variable

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    3

    Function - how to use variable

    I'm using the following code for a calculation script, which works fine:
    Code:
    function startCalc_percentage(){
      interval = setInterval("calc_percentage()",1);
    }
    
    function calc_percentage(){
      one = document.formulier.costbase_amount.value;
      two = document.formulier.costbase_margin.value; 
      document.formulier.amount.value = (one * 1) * (two / 100) + (one * 1);
    }
    The HTML code looks something like this:
    Code:
    <input type="text" name="costbase_amount" id="costbase_amount" value="" onFocus="startCalc_percentage();" onBlur="stopCalc();"/>
    <input type="text" name="costbase_margin" id="costbase_margin" value="" onFocus="startCalc_percentage();" onBlur="stopCalc();"/>
    <input type="text" name="amount" value=""/>
    I'm generating the HTML code in a loop (with PHP). What I want is to give each set of input fields a unique ID (with PHP), so I would have something like this:

    Code:
    <input type="text" name="costbase_amount[]" id="costbase_amount1" value="" onFocus="startCalc_percentage();" onBlur="stopCalc();"/>
    <input type="text" name="costbase_margin[]" id="costbase_margin1" value="" onFocus="startCalc_percentage();" onBlur="stopCalc();"/>
    <input type="text" name="amount[]" value=""/>
    
    <input type="text" name="costbase_amount[]" id="costbase_amount2" value="" onFocus="startCalc_percentage();" onBlur="stopCalc();"/>
    <input type="text" name="costbase_margin[]" id="costbase_margin2" value="" onFocus="startCalc_percentage();" onBlur="stopCalc();"/>
    <input type="text" name="amount[]" value=""/>
    
    (.. and so on ..)
    I could generate the javascript with PHP in a loop to get this to work. But is there a better way? Like giving the number as a variable to the script. I tried to get this to work, but due to my lack of js knowledge I didn't get it to work.

  2. #2
    Join Date
    Nov 2003
    Location
    Worthington, OH, USA
    Posts
    3,634
    This might help you:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
        <title>Test</title>
        <meta http-equiv="X-UA-Compatible" content="IE=7;FF=3;OtherUA=4">
    	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    var idNum = 0;
    function createElem() {
    	var elem = document.createElement("input");
    	elem.type= "text";
    	elem.id="elem"+idNum;
    	idNum++;
    	document.body.appendChild(elem);
    }
    
    </script>
    <body>
    <button onclick="createElem()>Create</button>
    </body>
    </html>
    54 68 65 42 65 61 72 4D 61 79

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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