javascript - AJAX URL update -
im sure there simple solution im having issues,
i have external ajax.js file has ajax call so:
$(function() { $.ajax({ url: url, success: function( data, status )
above ajax call have got 2 global variables called ticker , url so:
var ticker = 'aapl' var url = 'http://blahblahblah' + ticker
in html file have got input box user types in ticker symbol (e.g. goog) , clicks button update ticker variable ajax call done again new url.
<form id="form"> <h3>enter ticker symbol of company wish view:</h3> <input type="text" id="userinput"> <button onclick="myfunction()">try it</button> </form> <script type="text/javascript" src="ajax.js"> <script> function myfunction() { //old value alert(ticker); ticker = document.getelementbyid('userinput').value; //new value alert(ticker); } </script>
this code changes ticker variable ajax call still being performed old ticker url. think not being assigned properly? im new ajax cant find tutorials online explain problem.
thanks
as far can tell code you've posted, ajax routine being called when page loaded, explains why default url / ticker shown in results.
you need wrap ajax routine in function , call after user has inputted symbol:
in ajax.js file:
function getsymbolinfo(ticker) { // consider passing ticker function instead of using global var $.ajax({ url: url + ticker, success: function( data, status ) { // show results } }); }
and myfunction:
function myfunction() { var ticker = document.getelementbyid('userinput').value; getsymbolinfo(ticker); }
Comments
Post a Comment