Skip to content

Instantly share code, notes, and snippets.

@brookr
Created September 18, 2014 06:57
Show Gist options
  • Select an option

  • Save brookr/ec6617df5391fedac889 to your computer and use it in GitHub Desktop.

Select an option

Save brookr/ec6617df5391fedac889 to your computer and use it in GitHub Desktop.
Demo code from Class 5
<!DOCTYPE html>
<html>
<head>
<title>JavaScript &amp; jQuery - Chapter 7: Introducing jQuery - Looping</title>
<link rel="stylesheet" href="css/c07.css" />
</head>
<body>
<div id="page">
<h1 id="header">List</h1>
<h2>Buy groceries</h2>
<ul>
<li id="one" class="hot"><em>fresh</em> figs</li>
<li id="two" class="hot">pine nuts</li>
<li id="three" class="hot">honey</li>
<li id="four">balsamic vinegar</li>
</ul>
</div>
<script src="js/jquery-1.11.0.js"></script>
<script>
$(function() {
var originalBackgroundColor = $('li:first').css('background-color');
var newBackgroundColor = $('li:last').css('background-color');
$('ul').append('<p id="info"><input id="numberItems" type="text"></p>');
$('li').on({
mouseenter: function() {
$(this).css({
'background-color': newBackgroundColor,
'border': '1px solid #fff'
})
$('#info').text("Class is: " + $(this).attr("id"));
}, mouseleave: function() {
$(this).css({
'background-color': originalBackgroundColor,
'border-width': "0px"
})
}
})
$('input#numberItems').on('blur', function() {
var count = $(this).val();
$('li').hide();
$('li:lt(' + count + ')').fadeIn();
})
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment