Click event only displays data after 2nd click
I was looking at the jQuery.ajax() method to store a piece of JSON data in var d. I noticed in the console that after the initial button click, the var is stored but it will not display until the 2nd click. Can someone elaborate on this and propose a solution?
var d;
$(document).ready(function () {
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
}
});
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
});
});
<table>
<tr>
<td>Enter City</td>
<td><input type="text" id="City" /></td>
</tr>
<tr>
<td>Enter Country</td>
<td><input type="text" id="Country" /></td>
</tr>
</table>
<button id="Weather" >Get d</button>
<input type="text" id="Text" value="T °" />
<input type="text" id="Text1" value="T °" />
This is the JSON
"main": {
"temp": 37.38,
"pressure": 1030,
"humidity": 36,
"temp_min": 35.06,
"temp_max": 39.2
},
Thanks.
javascript jquery
add a comment |
I was looking at the jQuery.ajax() method to store a piece of JSON data in var d. I noticed in the console that after the initial button click, the var is stored but it will not display until the 2nd click. Can someone elaborate on this and propose a solution?
var d;
$(document).ready(function () {
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
}
});
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
});
});
<table>
<tr>
<td>Enter City</td>
<td><input type="text" id="City" /></td>
</tr>
<tr>
<td>Enter Country</td>
<td><input type="text" id="Country" /></td>
</tr>
</table>
<button id="Weather" >Get d</button>
<input type="text" id="Text" value="T °" />
<input type="text" id="Text1" value="T °" />
This is the JSON
"main": {
"temp": 37.38,
"pressure": 1030,
"humidity": 36,
"temp_min": 35.06,
"temp_max": 39.2
},
Thanks.
javascript jquery
add a comment |
I was looking at the jQuery.ajax() method to store a piece of JSON data in var d. I noticed in the console that after the initial button click, the var is stored but it will not display until the 2nd click. Can someone elaborate on this and propose a solution?
var d;
$(document).ready(function () {
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
}
});
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
});
});
<table>
<tr>
<td>Enter City</td>
<td><input type="text" id="City" /></td>
</tr>
<tr>
<td>Enter Country</td>
<td><input type="text" id="Country" /></td>
</tr>
</table>
<button id="Weather" >Get d</button>
<input type="text" id="Text" value="T °" />
<input type="text" id="Text1" value="T °" />
This is the JSON
"main": {
"temp": 37.38,
"pressure": 1030,
"humidity": 36,
"temp_min": 35.06,
"temp_max": 39.2
},
Thanks.
javascript jquery
I was looking at the jQuery.ajax() method to store a piece of JSON data in var d. I noticed in the console that after the initial button click, the var is stored but it will not display until the 2nd click. Can someone elaborate on this and propose a solution?
var d;
$(document).ready(function () {
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
}
});
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
});
});
<table>
<tr>
<td>Enter City</td>
<td><input type="text" id="City" /></td>
</tr>
<tr>
<td>Enter Country</td>
<td><input type="text" id="Country" /></td>
</tr>
</table>
<button id="Weather" >Get d</button>
<input type="text" id="Text" value="T °" />
<input type="text" id="Text1" value="T °" />
This is the JSON
"main": {
"temp": 37.38,
"pressure": 1030,
"humidity": 36,
"temp_min": 35.06,
"temp_max": 39.2
},
Thanks.
javascript jquery
javascript jquery
asked Nov 16 '18 at 6:55
MacMac
781212
781212
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
With if/else outside the AJAX call, it gets executed immediately after sending the call i.e. d
is undefined then and both conditions fails.
Hence, move if/else inside success callback as the value of d
has been updated by then.
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
}
});
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332850%2fclick-event-only-displays-data-after-2nd-click%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
With if/else outside the AJAX call, it gets executed immediately after sending the call i.e. d
is undefined then and both conditions fails.
Hence, move if/else inside success callback as the value of d
has been updated by then.
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
}
});
});
add a comment |
With if/else outside the AJAX call, it gets executed immediately after sending the call i.e. d
is undefined then and both conditions fails.
Hence, move if/else inside success callback as the value of d
has been updated by then.
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
}
});
});
add a comment |
With if/else outside the AJAX call, it gets executed immediately after sending the call i.e. d
is undefined then and both conditions fails.
Hence, move if/else inside success callback as the value of d
has been updated by then.
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
}
});
});
With if/else outside the AJAX call, it gets executed immediately after sending the call i.e. d
is undefined then and both conditions fails.
Hence, move if/else inside success callback as the value of d
has been updated by then.
$('#Weather').click(function () {
var requestData = $('#City').val() + ',' + $('#Country').val();
var unit = 'imperial';
var key = '..........................';
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
method: 'get',
data: { q: requestData, units: unit, APPID: key },
dataType: 'json',
success: function (data) {
d = data.main.temp;
if (d >= 40) {
document.getElementById("Text").value = "Value = " + d + " °";
}
else if (d < 40) {
document.getElementById("Text1").value = "Value = " + d + " °";
}
}
});
});
answered Nov 16 '18 at 6:57
Nikhil AggarwalNikhil Aggarwal
24.1k32848
24.1k32848
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332850%2fclick-event-only-displays-data-after-2nd-click%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown