Skip to content

Instantly share code, notes, and snippets.

@mojaray2k
Last active December 22, 2015 15:09
Show Gist options
  • Save mojaray2k/6490665 to your computer and use it in GitHub Desktop.
Save mojaray2k/6490665 to your computer and use it in GitHub Desktop.
We can now write some jQuery to demonstrate what the second argument does. The context that we give in the first case, is roughly equivalent to using the find() method. The second example saves us from having to call each of the methods individually.
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="We can now write some jQuery to demonstrate what the second argument does:" />
<meta charset=utf-8 />
<title>jQuery Function Second Argument</title>
</head>
<body>
<ul id="firstList">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<ul id="secondList">
<li>blue</li>
<li>green</li>
</ul>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</body>
</html>
// Select an element. The #firstList is the context that limit the search.
// You can use a selector, a jQuery object or a dom element
$('li', '#firstList').each(function() {
console.log($(this).html());
});
console.log('-----');
// Create an element. The second argument is an
// object with jQuery methods to be called.
var div = $('<div>', {
"class": "bigBlue",
"css": {
"background-color": "purple"
},
"width": 20,
"height": 20,
"animate": { // You can use any jQuery method as a property!
"width": 200,
"height": 50
}
});
div.appendTo('body');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment