Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem you are experiencing is that you dont do anything to stop your form from being submitted, thus it submits the form before it gets a chance to get the data from the worldweatheronline.com.</p> <p>Change your click handler to be like this:</p> <pre><code> $(".button").click(function(event) { event.preventDefault(); </code></pre> <p>Here is my completed sample code that works the way you want it to:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'&gt;&lt;/script&gt; &lt;script type='text/javascript'&gt; jQuery(document).ready(function() { $('.error').hide(); $(".button").click(function(event) { event.preventDefault(); // validate and process form here $('.error').hide(); var zipCode = $("input#name").val(); if (zipCode == "") { $("label#name_error").show(); $("input#name").focus(); return false; } var key = 'ac9c073a8e025308101307'; var noOfDays = 5; var url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=' + zipCode + '&amp;format=json&amp;num_of_days=' + noOfDays + '&amp;key=' + key; alert('first'+url); jQuery.get(url, function(test) { alert(url); $.each(test.data.weather, function(i, item){ $("body").append("&lt;p&gt;Date: "+item.date+"&lt;/p&gt;"); if ( i == 3 ){ return false; } }); }, 'jsonp'); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="contact_form"&gt; &lt;form name="contact" action=""&gt; &lt;fieldset&gt; &lt;label for="name" id="name_label"&gt;Name&lt;/label&gt; &lt;input type="text" name="name" id="name" size="30" value="" class="text-input" /&gt; &lt;label class="error" for="name" id="name_error"&gt;This field is required.&lt;/label&gt; &lt;br /&gt; &lt;input type="submit" name="submit" class="button" id="submit_btn" value="Send" /&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload