Max Heffern Max Heffern - 6 months ago 12
Javascript Question

.val() not assigning return to new variable

After this zip will remain at 0 no matter what the value of "input" is. I suspect it has something to do with apiAddress not updating zip after the new value is assigned to zip. Can any one explain what is happening?

$(document).ready(function() {
zip = 0;
apiAddress = "api.openweathermap.org/data/2.5/weather?zip=" + zip;

$("#submit").on("click", function() {
zip = $("input").val();
$("#temperature").html(apiAddress);
});
});

Answer

You are adding the zip variable to your url in the wrong place. Also, it's better to care about variable scope. Try this:

$(document).ready(function() {
    var zip = 0;

    $("#submit").on("click", function() {
        zip = $("input").val();
        apiAddress = "api.openweathermap.org/data/2.5/weather?zip=" + zip;
        $("#temperature").html(apiAddress);
    });
}); 
Comments