I want the user to be able to change a part of the URL address to their post code. I have a text box and button which I am hoping will submit the value to the URL.
jQuery:
jQuery(document).ready(function($) {
$.ajax({
url : "/" + 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 : "/" + PostCode + ".json",
does not work that way, but I didn't know what else to do. Could someone please show me what I need to do in order for this to work?
I want the user to be able to change a part of the URL address to their post code. I have a text box and button which I am hoping will submit the value to the URL.
jQuery:
jQuery(document).ready(function($) {
$.ajax({
url : "http://SomeAddress./" + 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./api/2508132ae0c7601a/geolookup/conditions/q/UK/" + PostCode + ".json",
does not work that way, but I didn't know what else to do. Could someone please show me what I need to do in order for this to work?
Share Improve this question edited Dec 27, 2012 at 3:21 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Dec 27, 2012 at 3:14 ChristopherStrydomChristopherStrydom 8,1765 gold badges24 silver badges35 bronze badges 4- 2 The only reason that wouldn't work were if PostCode wasn't global.. If it was your sorted – cjds Commented Dec 27, 2012 at 3:19
-
2
Why don't you do the
$.ajax
after the linevar PostCode
inside the click handler? – BalusC Commented Dec 27, 2012 at 3:20 -
1
+1 @CarlSaldanha You set your
PostCode
variable in the click function therefore, you can't access it from other places. – wakooka Commented Dec 27, 2012 at 3:21 - Well that was simple... Thanks Guys – ChristopherStrydom Commented Dec 27, 2012 at 3:43
1 Answer
Reset to default 9jQuery(document).ready(function($) {
var PostCode=1;
$.ajax({ url : "http://SomeAddress./"+PostCode +".json",
dataType : "jsonp"
//.... more stuff
});
$("#SetPostCode").click(function() {
PostCode = document.getElementById("GetPostCode").value;
$("#GetPostCode").val(" ");
return false;
});
});
Above would work as PostCode is now global to the jQuery and can be accessed anywhere