jquery - How can I get these two javascript codes to work together? -


i made form using html.

at first had simple. input amount, user enter. made javascript code calculate dynamic price based on user's amount input. code follows:

<input class="typeahead" type="text" placeholder="amount" name="gift-card amount"/> 

the javascript:

jquery("input[name='gift-card amount']").change(function () {      if (isnan(parsefloat(this.value)) || !isfinite(this.value)) {           jquery(this).val('');           return false;    }         var calc = parsefloat(this.value) * 0.95;         jquery(this).parents("form").find("input[name='price']").val(calc);     }); 

the calculation constant 0.95. added new input. store name. user enter store name. amount:

<input class="stores typeahead" type="text" placeholder="stores" name="name"/> 

and want price change based on both store name , amount. created object:

var stores = {     "mcdonalds" : .90,     "target" : .92, }  var storename = jquery(this).parents("form").find("input[name='name']").val();  console.log(stores[storename]); 

so instead of constant 0.95, value can replaced preset values based on store name entered. don't know how 2 work together. meaning, how recode first javascript recornize var store values instead of 0.95?

jquery("input[name='gift-card amount']").change(function () {     var amount = parsefloat(this.value);     if (isnan(amount) || !isfinite(amount)) {         jquery(this).val('');         return false;     }     var storename = jquery(this).parents("form").find("input[name='name']").val();     if (storename in stores) {         var calc = amount * stores[storename];         jquery(this).parents("form").find("input[name='price']").val(calc);     } }); 

i suggest change stores text input <select>. way, don't depend on user spelling store correctly, including capitalization.

<select name="name" class="storename">     <option value="">please select store</option>     <option value=".90">mcdonalds</option>     <option value=".92">target</option> </select> 

then can use

var calc = parsefloat(jquery(this).parents("form").find(".storename").val()); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -