How to add a variable to the URL $ .ajax ({url: ""});

I want the user to be able to change part of the URL to his zip code. I have a text box and a button that I hope will pass the URL value.

JQuery

jQuery(document).ready(function($) { $.ajax({ url : "http://SomeAddress.com/" + PostCode + ".json", dataType : "jsonp", success : function(parsed_json) { 

HTML:

 <input type="text" id="GetPostCode" /> <button id="SetPostCode">Set This Post Code</button> 

JQuery

 $("#SetPostCode").click(function() { var PostCode = document.getElementById("GetPostCode").value; $("#GetPostCode").val(" "); return false; }); 

I understand that the line

 $.ajax({ url : "http://api.wunderground.com/api/2508132ae0c7601a/geolookup/conditions/q/UK/" + PostCode + ".json", 

not working, but I did not know what else to do. Can someone please show me what I need to do to make this work?

+4
source share
1 answer
 jQuery(document).ready(function($) { var PostCode=1; $.ajax({ url : "http://SomeAddress.com/"+PostCode +".json", dataType : "jsonp" //.... more stuff }); $("#SetPostCode").click(function() { PostCode = document.getElementById("GetPostCode").value; $("#GetPostCode").val(" "); return false; }); }); 

It would work better since PostCode is now global for jQuery and can be accessed anywhere

+6
source

All Articles