-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finishing class 3 with exercise involving retrieving values from html…
… forms
- Loading branch information
1 parent
f0451bc
commit 1ede7c0
Showing
6 changed files
with
208 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
.box{ | ||
width: 100px; | ||
height: 200px; | ||
width: 200px; | ||
height: 100px; | ||
border: 1px solid #ccc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<html> | ||
<head> | ||
<title>My Site!</title> | ||
<link rel="stylesheet" href="style.css"> | ||
<script src = "http://code.jquery.com/jquery-1.8.3.min.js"></script> | ||
<script src = "javascript.js"></script> | ||
</head> | ||
<body> | ||
My Site!</br> | ||
<div class = "box"> | ||
</div> | ||
<div class = "box"> | ||
</div> | ||
<form id = "calculate"> | ||
<input type = "text" id = "snack" placeholder = "Your favorite snack"/><br/> | ||
<input type = "text" id = "age" placeholder = "Your age"/><br/> | ||
<input type = "text" id = "times-per-day" placeholder = "How many times a day?"><br/> | ||
<input type = "submit" value = "Calculate Lifetime Supply!"/> | ||
</form> | ||
<div id = "lifetime-supply"> | ||
|
||
</div> | ||
<div id = "favorite-things"> | ||
<p>My favorite things:</p> | ||
</div> | ||
<form id = "favorites"> | ||
<input type = "text" id = "thing" placeholder = "What's one of your favorite things?"/><br/> | ||
<input type = "submit" value = "Add to list!"/> | ||
</form> | ||
|
||
<form id = "friends"> | ||
<input type = "text" id = "friend-name" placeholder = "Friend's name"/><br/> | ||
<input type = "text" id = "friend-hair" placeholder = "Friend's hair color"/><br/> | ||
<input type = "submit" value = "Add to list!"/> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
$(document).ready(function(){ | ||
$('.box').bind({ | ||
click: function() { | ||
$(this).css('background-color', 'green') | ||
$(this).html('Clicked!') | ||
}, | ||
mouseenter: function() { | ||
$(this).css('background-color', 'purple') | ||
$(this).html('Hi!') | ||
}, | ||
mouseleave: function(){ | ||
$(this).css('background-color', 'orange') | ||
$(this).html('Bye!') | ||
} | ||
}); | ||
$('#calculate').submit(function(event){ | ||
var givenAge = $('#age').val(); | ||
var givenSnack = $('#snack').val(); | ||
var givenPerDay = $('#times-per-day').val(); | ||
$('#lifetime-supply').html(calculate(givenAge, givenSnack, givenPerDay)); | ||
return false; | ||
}); | ||
$('#favorites').submit(function(event){ | ||
var givenThing = $('#thing').val(); | ||
favoriteThings(givenThing); | ||
return false | ||
}); | ||
$('#friends').submit(function(event){ | ||
var name = $('#friend-name').val(); | ||
var hair = $('#friend-hair').val() | ||
var friend = {name: name, hair:hair}; | ||
myFriends(friend); | ||
return false; | ||
}); | ||
}) | ||
|
||
function calculate(age, snack, perDay){ | ||
var oldAge = 96; | ||
|
||
var days = (oldAge - age) * 356; | ||
var total = perDay * days; | ||
if(total > 40000){ | ||
return "You will need " + total + " of " + snack + " to last you until the ripe old age of " + oldAge + ". Wow! That's a lot!"; | ||
}else{ | ||
return "You will need " + + total + " of " + snack + " to last you until the ripe old age of " + oldAge + ". You seem pretty reasonable"; | ||
} | ||
} | ||
|
||
function favoriteThings(thing){ | ||
$('#favorite-things').append('<p>'+ thing +'</p>'); | ||
} | ||
function myFriends(friend){ | ||
var resultDiv = $('<div></div>') | ||
var resultParagraph = $('<p>' + describeFriend(friend) + '</p>'); | ||
resultDiv.append(resultParagraph); | ||
$('body').append(resultDiv); | ||
} | ||
function describeFriend(friend){ | ||
return "My friend " + friend.name + " has " + friend.hair + " hair. "; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.box{ | ||
width: 200px; | ||
height: 100px; | ||
border: 1px solid #ccc; | ||
} | ||
|
||
input{ | ||
width: 200px; | ||
padding: 5px; | ||
margin: 5px; | ||
} |