arrays - Javascript code-- What's wrong with function ToppingsWanted()? -


i'm having trouble part of javascript assignment. involves using couple of arrays: 1 elements, 1 without seen below. i've included code necessary arrays work you're not left in dark details , explain go.

below 2 arrays. array 'toppings' filled elements appear in list box later on. array below it, 'selectedtoppings' empty array hold elements user selects list box.

var toppings = [ "sausage" , "pepperoni" , "extra cheese" , "onions" , "black olives" , "anchovies" , "pineapple" , "canadian bacon" , "ground beef" ];  var selectedtoppings = []; 

the function below output of program. when first wrote it, didn't display because none of function calls right. i've been doing putting semi-colon after each function call , making sure appropriate function working correctly before moving on next one. right i'm stuck on function toppingswanted(). you'll notice semi-colon after "amount due: "--this semi-colon referred before. i'm pretty sure function call correct because functions quantity() , size(), come before work perfectly.

function thankyou() { document.pizzaform.ordertext.value="thank order bhc pizza!\nyou have ordered " + quantity() + " " + size() + " pizza(s) with\n" + toppingswanted() + "amountdue ";  } 

the function toppingswanted() see below what's giving me grief. i've tried numerous ways work, nothing happens. i've visited site, checked w3c schools, couldn't find answers how make work. @ point don't know enough javascript arrays know what's wrong here. needs happen this: when user selects topping list box mentioned earlier (code shown below function), appropriate element taken toppings array , put selectedtoppings array, , function call display each element selectedtoppings array on own line. there aren't syntax errors according dreamweaver, can't tell if there logic errors. help?

function toppingswanted() { (var i=0; i<10; i++); {     if (document.pizzaform.lstingredients.toppings[i].selected==true)         selectedtoppings.push[i]; } var topsselect=selectedtoppings.join("\n"); return topsselect; }  <select name="lstingredients" size="9" multiple="multiple">         <option id="toppings[0]" value="1.00">sausage</option>         <option id="toppings[1]" value="1.00">pepperoni</option>         <option id="toppings[2]" value="1.00">extra cheese</option>         <option id="toppings[3]" value="1.00">onions</option>         <option id="toppings[4]" value="1.00">black olives</option>         <option id="toppings[5]" value="1.00">anchovies</option>         <option id="toppings[6]" value="1.00">pineapple</option>         <option id="toppings[7]" value="1.00">canadian bacon</option>         <option id="toppings[8]" value="1.00">ground beef</option>     </select> 

the following code should solve problem. paste in body of document

  1. first need bind toppingswanted() function onchange event of list. when state of list changes function gets fired.

  2. rather loop through list elements can selected 1 using selectedindex property.

  3. i have changed values indices. function value list , index 0-8 , push corresponding value index toppings array selected toppings array.

    <form name="pizzaform">     <select name="lstingredients" size="9" multiple="multiple" onchange="toppingswanted()">         <option id="toppings[0]" value="0">sausage</option>         <option id="toppings[1]" value="1">pepperoni</option>         <option id="toppings[2]" value="2">extra cheese</option>         <option id="toppings[3]" value="3">onions</option>         <option id="toppings[4]" value="4">black olives</option>         <option id="toppings[5]" value="5">anchovies</option>         <option id="toppings[6]" value="6">pineapple</option>         <option id="toppings[7]" value="7">canadian bacon</option>         <option id="toppings[8]" value="8">ground beef</option>     </select>    </form>  <script type="text/javascript">     var selectedtoppings = [];      var toppings = [ "sausage" , "pepperoni" , "extra cheese" , "onions" ,"black olives" , "anchovies" , "pineapple" , "canadian bacon" , "ground beef" ];      function toppingswanted()     {          var ingredients = document.pizzaform.lstingredients;          var chosenoption = ingredients.options[ingredients.selectedindex];          selectedtoppings.push(toppings[chosenoption.value]);          alert(selectedtoppings);                         return selectedtoppings     }  </script> 

a few notes.

  1. this pretty redundant, can store values storing in toppings array in list option values themselves. way if toppings ever change don't have update 2 things.

  2. this code not ensure toppings aren't added twice. create cheese pizza.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -