calling two fucntions from one event (onchange)

Discussion in 'JavaScript' started by jacka, May 24, 2007.

  1. #1
    Hi

    I want to try to call two function with one onchange event.
    I wonder if it can be done or whats the alternative.

    I have a list in html that needs both return a value and keep teh chosen item in the list selected, but don't know how to do both.
    plz see coeds below:

    ------------
    <?php


    while($row = mysql_fetch_array($result))


    {



    // Increment the total cost of all items

    $totalCost += ($row["qty"] * $row["price_each1"]);
    ?>
    <tr>
    <td width="7%" height="25">
    <font face="verdana" size="1" color="black">
    <select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
    <?php



    if ($startnumber==0)
    {
    $startnumber=1;
    $endnumber=5;
    }



    for($i = $startnumber; $i <= $endnumber; $i++)
    {
    echo "<option ";
    if($row["qty"] == $i)
    {
    echo " SELECTED ";
    }
    echo ">" . $i . "</option>";
    }

    ?>
    </select>
    </font>
    </td>

    <td width="10%" height="25">
    <font face="verdana" size="1" color="black">
    &pound;<?php echo number_format($row["price_each1"], 2, ".", ","); ?> </font> </td>

    <td width="23%" height="25" >
    <font face="verdana" size="1" color="red">


    <select name="price_break" size="1" onChange="range(this)">


    <?php








    $c=$row["price_each1"];// price for single item




    for($i = 1; $i <= 8; $i++)
    {

    $b=$row["price_each".$i];

    $d=(($c-$b)/$c)*100;






    echo "<option> ";






    echo $row[("price_break".$i)]. " ," .round($d)."%" . "</option>";





    }

    ?>

    and these are the two functions:

    function UpdateQty(item)
    {
    itemId = item.name;
    newQty = item.options[item.selectedIndex].text;

    document.location.href = 'cartnew.php?action=update_item&id='+itemId+'&qty='+newQty;
    }





    function range(x)

    {



    var string = x.options[x.selectedIndex].text;

    //document.write((string.split("-",1)) + "<br />")

    var stringArray=string.split("-");

    //document.write ((stringArray[0]) + "<br />");
    var start=(stringArray[0]);

    //document.write(start);

    var end1=(stringArray[1]);
    //document.write(end1);

    var endstringArray=end1.split(" ");
    var end=(endstringArray[0]);
    //document.write(end);

    // document.write (optionNumber);

    //var selObj = document.getElementById('price_break');
    //var txtIndexObj = document.getElementById('txtIndex');
    //var selIndex = selObj.selectedIndex;
    //document.write(selIndex);


    document.location.href = 'cartnew.php?start='+start+'&end='+end;

    //var optionNumber=document.price_break.selectedIndex;
    }
     
    jacka, May 24, 2007 IP
  2. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi
    I have now found out that I can put more than one fucntion for an event this way:
    <select name="price_break" size="1" onChange="range(this);update(z);">

    But why doens';t it do both jobs?

    they both do their own task individually but not when put togther. I.e. keep the chosen list selcted and pass the start and stop values back to the form?
    :confused:
    thanks
     
    jacka, May 24, 2007 IP
  3. davecardwell

    davecardwell Peon

    Messages:
    33
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It won’t work, because both functions change the document location, meaning the second function will not be called as the first causes the browser to navigate away from the current page (or possibly—but not likely—depending on the browser implementation, the second overwrites the first).

    Your options appear to be:
    1). use AJAX to make at least one of the submissions
    2). use JavaScript to create some hidden <input> elements containing the values you want, and do a form submit rather than changing document.href
     
    davecardwell, May 24, 2007 IP
  4. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi
    Thanks for the reply.

    I will try to do the 2nd option, as I do not want to learn a new language. since I am a newbie alreay.
    Thanks for taking the time to study my problem.
    jacka
     
    jacka, May 29, 2007 IP
  5. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi Dave

    i don't suppose you could give me a sample of code that I could use to alter to my specific needs, could you? ( as I am very new to javascript)

    Thanks
    jacka
     
    jacka, May 29, 2007 IP