javascript - passing variables to function with jquery -
i'm new javascript although use php.
i'm having issues passing variables using javascript/jquery on onclick event on href.
i'm pulling json data remote url , there 3 parameters need go each request.
date, price , currency.
i have function build url looks this:
function getindexdata(date, price, currency){ ajaxurl = "/data/"+date+"/"+price+"/"+currency+""; }
this works , builds url need.
however, have jquery datepicker on page change date whatever want looks like:
$(function () { $("#datepicker").datepicker( { dateformat: 'yymmdd', onselect: function (date) { getindexdata(date, price, currency ) } } ); });
even if price , currency have been set 'undefined'.
i have same issue onclick events - if try send price or currency using (for example):
<a href="javascript:void(0);" onclick="getindexdata('date','price','currency')">net</a></span>
price , currency undefined.
is there way can send 1 of variables function whilst retaining existent values?
apologies must basic question. i'm new , i've misunderstood along way.
ok here updated answer. can see, if variables declared in global scope of code, date picker function have access it. think did not work because html wrong. deleted of html , included date picker , if test in browser, see in console price, currency variables available inside date picker onselect function , can execute function getindexdata inside it. i've assigned function variable way can use variable getindexdata if want use function.
<html> <head> <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgfzhoseeamdoygbf13fyquitwlaqgxvsngt4=" crossorigin="anonymous"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <div class="col-sm-5 index-grid"> <span class="grid-label">price</span> <span class="btn btn-grid"><a href="javascript:void(0);" onclick="getindexdata(indexdate, indexcurrency ,indexprice);">close</a></span> <span class="btn btn-grid"><a href="javascript:void(0);" onclick="getindexdata(indexdate, indexcurrency ,indexprice);">return</a></span> <span class="btn btn-grid"><a href="javascript:void(0);" onclick="getindexdata(indexdate, indexcurrency ,indexprice);">net</a></span> </div> <p>date: <input type="text" id="datepicker"></p> </body> </html> <script> var ajaxurl; var indexdate = "2017-08-20" var indexprice = "closeprice"; var indexcurrency = "eur"; var getindexdata = function (indexdate, indexcurrency, indexprice) { ajaxurl = "/data/" + indexdate + "/" + indexprice + "/" + indexcurrency + ""; alert(ajaxurl); } $(document).ready(function() { $(function() { $("#datepicker").datepicker({ dateformat: 'yymmdd', onselect: function(date) { console.log(date); console.log(indexprice); console.log(indexcurrency); console.log(getindexdata); } }); }); }); </script>
Comments
Post a Comment