javascript - Pass value To text box using json -
i'm trying access data js file, , fetch value in html text box, i'm not able result. here samle.js file , jsonhtml.html file
{ "var1":"1"; "var2":"2"; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $(document).ready(function() { $.ajaxsetup({cache: false}); setinterval(function () { $.getjson("sample.js", function (data) { if (data.var1) { $('#c1-cycle').val(data.var1); } if (data.var2) { $('#c2-cycle').val(data.var2); } }); }, 1000); }); })(jquery); </script> </head> <body> <div> <input type="text" id="c1-cycle"> </div> <hr> <div> <input type="text" id="c2-cycle-cycle"> </div> </body </html>
use script:
$(document).ready(function() { $.ajaxsetup({cache: false}); var winterval=setinterval(function () { $.getjson("sample.js", function (result) { var data=$.parsejson(result); if (data.var1) { $('#c1-cycle').val(data.var1); } if (data.var2) { $('#c2-cycle-cycle').val(data.var2); } }); }, 1000); });
but make sure, in sample.js file data as:
{"var1":"1","var2":"2"}
example as: https://jsfiddle.net/5881pnqn/2/
Comments
Post a Comment