javascript - making two scripts work together for a dropdown menu -


i have html code

<html> <head> <script type="text/javascript">     window.onload = function(){     document.getelementbyid("shipping-method").onchange = function(){         window.scrollto(0, 0);     }; }; </script> <script> function calc() { var subtotal = parsefloat(document.getelementbyid("subtotal").innerhtml.substring(1)); var shipping = parseint(document.getelementbyid("shipping-method").value); if(shipping == 1) {     var total = subtotal+6.95;     document.getelementbyid("shipping").innerhtml = "$"+6.95; } else {     var total = subtotal+17.95;     document.getelementbyid("shipping").innerhtml = "$"+17.95; } document.getelementbyid("total").innerhtml = "$"+total; } </script> </head> <body> <select  onchange="calc()" class="shipping-method" id="shipping-method"> <option value="">-choose shipping method-</option> <option selected="selected" value="1">normal shipping - $6.95</option> <option value="2">priority shipping - $17.95</option>  </select> <div class="calculations"> <table>  <tbody><tr>     <td>subtotal:</td>     <td id="subtotal">$97.00</td> </tr>   <tr>     <td>shipping:</td>     <td id="shipping">$6.95</td> </tr>    <tr class="total">     <td>total:</td>     <td id="total">$103.95</td> </tr> </tbody></table> </div> </body> </html> 

the dropdown menu @ bottom of webpage, use first script to user top of page after selecting 1 of options , getting total, both scripts don't work together, have remove 1 of them other work, how make both scripts work without conflict, thanks.

you overriding onchange function. if want 2 things, put them both in onchange function, don't assign twice.

here example code (shorted brevity).

<html> <head><title>example</title></head> <body> <select id="shipping-method"></select> <table></table> <script type="text/javascript">     function calc() {         // calculations here     }     document.getelementbyid("shipping-method").onchange = function(){         window.scrollto(0, 0); // scroll top         calc(); // call function     }; </script> </body> </html> 

note put javascript @ bottom avoid accessing element not exist.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -