I have an input field to enter a city name - the on click function for that fills in certain div's. I want to local storage every input (maybe in an array) and every input value dynamically gets created into a button. The buttons are the 'city' name inputs - on click of these buttons will repopulate the div data with that city name and its' info. I am saving the city input as a variable and using that city name to be the id of each button. So how can I use a variable as my query selector?
HTML
<div class="city-search">Search For A City:
<input type="text" id="cityInput" style="text-transform: uppercase" />
<button class="searchBtn" type="submit"><span class="oi oi-magnifying-glass"></span></button>
</div>
<!-- This shows the search history and is reclickable -->
<div class="city-history" id="city-history">
</div>
<div>
<h2 id="city"></h2>
</div>
JQuery
$(".searchBtn").on('click', function getWeather () {
var cityName = $('#cityInput').val().toUpperCase();
$("#city").text("The weather in " + cityName);
// Create buttons for city inputs
var cityHistory = function() {
var cityH = document.createElement("button");
cityH.textContent = cityName;
cityH.setAttribute("id", cityName);
var history = document.getElementById("city-history");
history.appendChild(cityH);
}
cityHistory();
// Update city name with name on button
$(`#${cityName}`).on('click', function historyWeather () {
$('#city').text("The weather in " + cityName);
});
$("#city"), nor doesconsole.log(cityName)show the city name of the button.$('#'+cityName).on(...);as the query selector? That's what I usually do.#city) selector, but your element ish2.city.})in your code, but that will definitely break everything.