0

I have the following code that uses a weather plugin (https://a12k.io/reallysimpleweather), my problem is that I am wondering how to use the data that is gathered from a user via the HTML form element like below,

<script>var input = document.getElementById('cityInput').value;</script>

<script>
reallySimpleWeather.weather({
  //var city = "Bend, Or";
  wunderkey: '', // leave blank for Yahoo API
    location: input,
    woeid: '', // "Where on Earth ID" optional alternative to location
    unit: 'f', // 'c' also works
    success: function(weather) {
      html = '<h2>'+weather.temp+'°'+weather.units.temp+'</h2>';
      html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
      html += '<li>'+weather.currently+'</li>';
      html += '<li>'+weather.wind.direction+' '+weather.wind.speed+' '+weather.units.speed+'</li></ul>';
    document.getElementById('weather').innerHTML = html;
    },
    error: function(error) {
    document.getElementById('weather').innerHTML = '<p>'+error+'</p>';
    }
});
</script>


</head>
<body>
<div class="container-fluid">
<div id="weather"></div>

<form>
City: <input type="text" name="city" id="cityInput">
<input type="submit" value="Submit">
</form>

</div>
</body>
</html>   

I'm unsure as to if my issue is caused because of how the page and the API I am calling might be loading, but when I open it all up in a browser I am currently just getting an error from the plugin that says "There is a problem receiving the latest weather. Try again." Im not sure if this is caused because it is not able to update itself once it is initially loaded or if its because I am incorrectly storing the variable wrong in

 location: input,

Whenever I hit "submit" nothing changes expect the URL itself changes to add city=%27Bend%2C+OR%27 at the end or whatever the city entered is.

1
  • well you need to call it after the user enters the data.... Commented Oct 4, 2017 at 18:53

3 Answers 3

3

Get the value using JavaScript and put where you want:

<form action="begin-create-done.html" method="get">
    City: <input type="text" name="city" id="cityInput">
    <input type="submit" value="Submit">
</form>

In JavaScript:

var value = document.getElementByName( "city" ).value;
// or
// var value = document.getElementById( "cityInput" ).value;

reallySimpleWeather.weather({
    wunderkey: '',
    location: value,
    woeid: '',
    unit: 'f',
    success: function(weather) {
        html =  '<h2>'+weather.temp+'°'+weather.units.temp+'</h2>';
        html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
        html += '<li>'+weather.currently+'</li>';
        html += '<li>'+weather.wind.direction+' '+weather.wind.speed+' '+weather.units.speed+'</li></ul>';
        document.getElementById('weather').innerHTML = html;
    },
    error: function(error) {
        document.getElementById('weather').innerHTML = '<p>'+error+'</p>';
    }
});

Please, note that you will need to use the "id" attribute of the input tag to be able to catch this input through JavaScript. Not that inside your code will already used the document.getElementById() function. You will need an div to insert the html generated by the success function and this div must have the id attribute set to "weather".

Also, note that reallySimpleWeather.weather is a function that receives an object with various attributes. You can't set an variable inside this object. It is a syntax error. You must declare it before the reallySimpleWeather.weather call. Take a look above.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi and thanks for the response. This might be a "read the docs" type situation, but I have tried a few variations of that without luck. For example I have tried declaring the variable value, like you mentioned right at the start of the reallySimpleWeather.weather function above like, var value = document.getElementByName( "city" ).value; However this then removed the data altogether being displayed?
Its because you tried to declare a variable in a not allowed region. I'm updating my answer.
I also added in an edit to original to maybe give a little more clarification.
@jelidens take a look.
Do you want to keep calliing the reallySimpleWeather.weather function within a time interval?
|
1

I'd give the input an ID (e.g. <input type="text" name="city" id="city">)to make it easy to select say with:

var city = document.getElementById('city');

Then change the line in your plugin call to reference the value:

location: city.value,

1 Comment

Thank you for the help! I am currently working with this suggestion and others.
1

Give your input city an id and than you use getElementById to put that city in place for your location variable

1 Comment

Thank you for the help! I am currently reading into this and others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.