javascript - add 2 values and total them in one innerHTML -


i still learning js, , stuck once again, trying add 2 drop down menu values , total them in 1 innerhtml.

i trying take value2 phones/devices (item) add value new activation (item1) drop down , total them in commission innerhtml. in js, commission line cost.

i have tried adding element in cost line, , did not work. tried adding item cost line.

here html , js

    <table width="50%" border="0">   <tr>     <td width="20%"><div align="center">item</div></td>     <td width="20%"><div align="center">new activation</div></td>     <td width="20%"><div align="center">commission</div></td>     <td width="20%"><div align="center">price</div></td>     <td width="20%"><div align="center">meid</div></td>     <td width="20%"><div align="center">number</div></td>   </tr>          <td><div class="styled" align="center">             <select name="item" id="item">             <option value="">phones/devices</option>                 <option value="">-----apple-----</option>               <option value="200.00" value2="20.00" value3="iphone4">iphone 4 8gb</option>               <option value="225.00" value2="20.00" value3="iphone416">iphone 4 16gb</option>               <option value="250.00" value2="20.00" value3="iphone432">iphone 4 32gb</option>               <option value="275.00" value2="20.00" value3="iphone464">iphone 4 64gb</option>               <option value="300.00" value2="20.00">iphone 4s 8gb</option>               <option value="325.00" value2="20.00">iphone 4s 16gb</option>               <option value="350.00" value2="20.00">iphone 4s 32gb</option>               <option value="375.00" value2="20.00">iphone 4s 64gb</option>               <option value="400.00" value2="20.00">iphone 5</option>                 <option value="500.00" value2="30.00">iphone 5c</option>                 <option value="625.00" value2="30.00">iphone 5s</option>                 <option value="">-----blackberry-----</option>                 <option value="75.00" value2="5.00">curve</option>                 <option value="100.00" value2="5.00">bold</option>                 <option value="225.00" value2="10.00">q10</option>                 <option value="250.00" value2="10.00">z10</option>               <option value="">-----htc-----</option>                 <option value="100.00" value2="5.00">incredible</option>                 <option value="150.00" value2="5.00">incredible 2</option>                 <option value="150.00" value2="5.00">evo</option>                 <option value="125.00" value2="5.00">evo shift</option>                 <option value="200.00" value2="10.00">evo lte</option>                 <option value="175.00" value2="10.00">rhyme</option>                 <option value="225.00" value2="10.00">rezound</option>                 <option value="400.00" value2="20.00">dna</option>               <option value="450.00" value2="20.00">htc one</option>               <option value="600.00" value2="20.00">htc 1 m8</option>                 <option value="">-----huawei-----</option>                 <option value="100.00" value2="5.00">ascend y</option>                 <option value="150.00" value2="5.00">ascend plus</option>                 <option value="">-----lg-----</option>                 <option value="50.00" value2="5.00">optimus dynamic</option>                 <option value="100.00" value2="5.00">optimus zip</option>                 <option value="75.00" value2="5.00">vortex</option>                 <option value="100.00" value2="5.00">enlighten</option>                 <option value="250.00" value2="10.00">lucid</option>                 <option value="200.00" value2="10.00">revolution</option>                 <option value="250.00" value2="10.00">spectrum</option>                 <option value="450.00" value2="20.00">g2</option>                 <option value="">-----motorola-----</option>                 <option value="100.00" value2="5.00">droid 2</option>                 <option value="175.00" value2="5.00">droid 3</option>                 <option value="200.00" value2="10.00">droid 4</option>                 <option value="225.00" value2="10.00">bionic</option>                 <option value="250.00" value2="10.00">razr</option>                 <option value="275.00" value2="10.00">razr maxx</option>                 <option value="300.00" value2="10.00">razr hd</option>                 <option value="350.00" value2="20.00">moto x</option>                 <option value="300.00" value2="20.00">moto g</option>                 <option value="350.00" value2="20.00">droid ultra</option>                 <option value="">-----samsung-----</option>                 <option value="200.00" value2="10.00">galaxy stellar</option>                 <option value="250.00" value2="10.00">galaxy stratosphere 2</option>                 <option value="225.00" value2="10.00">galaxy nexus</option>                 <option value="225.00" value2="5.00">epic 4g</option>                 <option value="250.00" value2="10.00">galaxy s2</option>               <option value="300.00" value2="20.00">galaxy s3</option>               <option value="450.00" value2="20.00">galaxy s4</option>               <option value="650.00" value2="30.00">galaxy s5</option>                 <option value="450.00" value2="20.00">galaxy note ll</option>               <option value="600.00" value2="30.00">galaxy note lll</option>             </select>           </div></td>            <td><div class="styled" align="center">             <select name="item1" id="item1">             <option value="">new activation?</option>               <option value="5.00">yes</option>               <option value="0.00">no</option>              </select>           </div></td>          <span style="visibility:hidden;display:none;" id="phones"></span>             <td><div align="center">             <span id="cost"></span>           </div></td>           <td><div align="center">            <span id="price"></span>           </div></td>              <td>                 <div align="center">                    <input name="meid" type="text" id="meid"/>                 </div>             </td>               <td>                 <div align="center">                     <input name="number" type="text" id="number"/>                 </div>             </td>     </tr> 

and javascript

var item = document.getelementbyid('item'); var item1 = document.getelementbyid('item1');  item.onchange = function() {         price.innerhtml = "$" + this.value;         cost.innerhtml = "$" + (this[this.selectedindex].getattribute('value2'));         phone.innerhtml = "" + (this[this.selectedindex].getattribute('value3'));       }; 

and here jsfiddle

after messing around modified script think line not working properly

var commission = +phoneel.getattribute("value2"); 

i cant figure out wrong. http://jsfiddle.net/3zf8g/4/

here:

var phoneel = document.getelementbyid('item'); var activationel = document.getelementbyid('item1');  function updateprice() {     var phoneprice = +phoneel.value;      price.innerhtml = "$"+phoneprice; }  function updatecommission() {     var commission = +phoneel.options[phoneel.selectedindex].getattribute("value2"),         activationprice = +activationel.value,         totalcommission = commission + activationprice;      cost.innerhtml = "$" + totalcommission; }  phoneel.onchange = function() {     updateprice();     updatecommission(); };  activationel.onchange = function() {     updateprice();     updatecommission(); }; 

fiddle: http://jsfiddle.net/3zf8g/5/

you're not converting values stored in value , value2 attributes numbers; can parse string number preceding +. , you're not watching changes activation drop down. variable phone not defined. changed variable names sake of clarity.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -