Results 1 to 8 of 8

Thread: Javascript: passing parameters [Resolved]

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    Javascript: passing parameters [Resolved]

    I'm trying to pass a parameter to a function and then having the function output to the textbox that called it.

    Here is my calling function:

    Code:
    echo "<tr><td><p class=other>" . $row[0] . "</td>";
    echo "<td><p class=other><input type=text name=HourEntry" . $i ." size=10 onchange="Changehandler(i)"></td>";
    echo "<td><p class=other4>" . $row2[$i + 3] . "</td>";
    echo "<td><p class=other2><input type=text name=HourEntry2" . $i ." size=10></td></tr>";
    And here is my function:

    Code:
    <script language="JavaScript 1.2" type="text/javascript">
    <!--
    Function ChangeHandler(i)
    {
    	document.dform.HourEntry2.value=(parseFloat(HourEntry" + i + ".value)
    		- parseFloat(HourEntry2" + i + ".value));
    }
    //-->
    </script>
    I'm not sure if the function works, because I can't get the parser to get past the "Changehandler(i)" line when I call it. How do I pass the number of the textbox to the function??

    And in case you need to understand what is going on, the echo lines are in a loop creating approx. 30 text boxes and I need to have the user type text into the first one and have it update the next one.
    Last edited by ober0330; Dec 22nd, 2003 at 10:54 AM.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    ok... new calling line:

    Code:
    while($row = mssql_fetch_array($result)) 
    { 
    If ($row[0] != "Cell 20")
    {
    echo "<tr><td><p class=other>" . $row[0] . "</td>";
    echo "<td><p class=other><input type=text name=HEOne" . $i ." size=10 onchange=ChangeHandler(" . $i . ")></td>";
    echo "<td><p class=other><input type=text name=HETwo" . $i ." size=10 value=" . $row2[$i + 3] . "></td>";
    echo "<td><p class=other><input type=text name=HEThree" . $i ." size=10></td></tr>";
    }
    $i++;
    }
    New function:

    Code:
    <script language="JavaScript 1.2" type="text/javascript">
    <!--
    Function ChangeHandler(i)
    {
    	document.dform.HEThree" + i + ".value=(parseFloat(HEOne" + i + ".value) - parseFloat(HETwo" + i + ".value));
    }
    //-->
    </script>
    There were some definite mistakes that I made on the first try. Does anyone know what I'm doing wrong?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    off the top of my head if you call the function like this
    Code:
    ChangeHandler(this);
    then change the function to
    Code:
    //obj is the textbox
    ChangeHandler(obj){
     obj.value =(parseFloat(HEOne" + i + ".value) - parseFloat(HETwo" + i + ".value));
    }

  4. #4

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    But that won't work. That wouldn't get the correct value of "i" into the function. These textboxes are dynamic in name and that's what I need help in interfacing with.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    TA DA!!! Check this out!!

    Code:
    $i=0;
    while($row = mssql_fetch_array($result)) 
    { 
    If ($row[0] != "Cell 20")
    {
    echo "<tr><td><p class=other>" . $row[0] . "</td>";
    echo "<td><p class=other><input type=text name=\"HEOne" . $i ."\" size=10 onchange=\"ChangeHandler($i, document.dform.HEOne$i, document.dform.HETwo$i, document.dform.HEThree$i)\"></td>";
    echo "<td><p class=other><input type=text name=\"HETwo" . $i ."\" size=10 value=" . $row2[$i + 3] . "></td>";
    echo "<td><p class=other><input type=text name=\"HEThree" . $i ."\" size=10></td></tr>";
    }
    $i++;

    Code:
    <script language="JavaScript1.2" type="text/javascript">
    <!--
    Function ChangeHandler(someval, somebox0, somebox1, somebox2)
    {
    	somebox2.value=(parseFloat(somebox0.value) - parseFloat(somebox1.value))
    }
    -->
    </script>
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The function works? JavaScript is case sensitive, it should reject the incorrectly written Function keyword.

    But I guess the problem were the missing quotes on the HTML property value, right? The HTML rule is simple: you may only omit the quotes if the attribute value consists ONLY of numbers OR letters. The XHTML rule is even simpler: you may never omit the quotes

    I'm also surprised that PHP accepts the If.

    Don't forget that the document.name schema simply invites cross-browser troubles.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    I will change my capitalization.

    What is the correct way to reference a textbox in a form without using "document.documentname.item"?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Give the element an id attribute and use getElementById:
    var thebox = document.getElementById("id_of_box");

    Doesn't work in NS4, but neither does your current stuff.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width