<p>
How old are you?
<input id="their_age" name="their_age" /><a href="#" id="my_button"></a>
</p>
<p id="their_real_age"></p>
JavaScript
function some_function(their_age) {
var some_value = 2 + (their_age * 1);
return some_value;
}
$("#my_button").html("Let's see if you're lying!");
//Click binding
$("#my_button").click( function() {
var their_age = $("#their_age").val();
if (their_age > 0) {
var their_real_age = some_function(their_age);
$("#their_real_age").html("You lied! Your real age is " + their_real_age);
}else{
$("#their_real_age").html("Please enter a number!");
}
});
//Strings into an indexed array
var my_string1 = "this";
var my_string2 = "was";
var my_string3 = "silly.";
// Indexed array 0, 1, 2 etc.
var my_array = [my_string1, my_string2, my_string3];
//Array logging
console.log(my_array);
console.log(my_array[0]);
i = 0;
do {
console.log(my_array[i]);
i++;
}while (i < my_array.length);
var my_new_array = [my_string1, 1, "string", my_string3];
//var some_function_result = some_function();
//console.log(some_function_result);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.