Replace dots and commas in a field and then use that value to auto populate another field using jquery -
i'm using google maps calculate distance between cities. need use distance value basic calculations. distance has in "angloamerican" format (1,234.00) in metric system. so, google maps answer madrid - berlin query 1 of these two:
a) <span jscontent="distance['text']" jsdisplay="distance" jstcache="7">2.320,1 km</span> b) <span jscontent="distance.text" jstcache="23">2.320,1 km</span>
please notice differences in span "classes" (jstcache 7 or 23) , lack of "id" or "name" attributes.
what want accomplish is:
1) convert these google maps distance values "angloamerican" format (2,320 km) or (even better) format without thousands separator use dots decimal separator (2320.1 km)
2) use filtered value populate text field called distance
i searched on web, didn't find exact answer. post helpful:
populate hidden form element value value of text field on form submit (jquery)
it helped me bit auto-populate part, can't make work in combination google maps code. here current code:
<head> ... <script type="text/javascript"> function submitmyform(){ $('#distance').val($('jstcache^=7').val()); $('#googlemap').submit(); } </script> ... </head> <body> ... <form id="myform"> <input type="text" id="distance" name="distance" value="" /> </form> ... </body>
generally, can javascript string .replace() function. in case, it's little trickier because you're doing swap: periods commas, commas periods. means have use intermediate value. working example using underscores this:
<script type="text/javascript"> function submitmyform(){ var val = $('[jstcache=7]').text(); // text content of <span jstcache="7"> // replace using regex instead of strings, catch more first match val = val.replace(/\./g, "_"); val = val.replace(/,/g, "."); val = val.replace(/_/g, ","); $('#distance').val(val); $('#googlemap').submit(); } </script>
Comments
Post a Comment