I am making an AJAX call in my script which grabs HTML from the server. I need to replace my existing HTML with the returned data. The replacement works, but the variables lose their context, meaning I cannot fire the function to replace again.
This is what my JavaScript looks like:
(function($){
var $weatherCard = $(".weather-card");
var $reloadWeather = $(".reload-weather");
var $newLocation = $("#zip").val();
var $featureParent = $(".pb-f-global-weather-forecast");
$reloadWeather.on('click', function() {
var $newLocation = $("#zip").val();
changeLocation($newLocation);
});
function changeLocation(location) {
$.ajax({
"url" : $reloadWeather.data('path') + '?' +
'contentConfig={\"Type\":\"forecast\",' +
'\"City\":\"' + location + '\",' +
'\"Country\":\"USA\",' +
'\"Count\":\"5\"' + '}',
//'&customFields={\"enabledLoadMore\":true,' + '"layoutMode":' + '"' + $loadMoreButton.data('feed-layout') + '"' + ',' + '"dateType":' + '"' + $loadMoreButton.data('date-type') + '"' + ',' + '"hideSubhead":' + '"' + $loadMoreButton.data('show-sub') + '",' + '"hideDate":' + '"' + $loadMoreButton.data('show-date') + '",' + '"hideByline":' + '"' + $loadMoreButton.data('show-byline') + '"' + '}',
"timeout" : "1500",
"method": "GET",
"accept" : false
}).done(function(data) {
$featureParent.replaceWith(data.rendering);
});
};
In other words I want the variables listed at the top to be re-assigned to the same element once the replaceWith()
function is fired. Re-assigning the variables within the done function doesn't seem to work.