Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
jQuery is a library of JavaScript functions.
It contains many functions to help simplify your programming, including:
Download the library, store it locally:
<head>
<script src="jquery.js"></script>
</head>
Include the the live library from jQuery:
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>
</head>
Include the the live library from Google:
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</head>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script src="scripts.js"></script>
</head>
jQuery should go first because
our code will
depend on it later.
Are you done or really stuck?
Check out our example.
Keep in mind that this is one of many possible solutions.
Select some elements and do something with them.
The jQuery API Documentation is a great reference.
Before we used things like document.getElementById()
and document.getElementsByTagName()
In jQuery, we find elements using $()
In jQuery, we find elements using CSS selectors
$()
is an alias for
jQuery()
Find elements by id using #
<div id="my-data">
the data
</div>
<div id="important">
<p>important text</p>
</div>
$("#important");
$("#my-data");
Find elements by element name
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
<a id="gdi-link" href="http://girldevelopit.com">
Girl Develop It
</a>
$("li");
$("a");
Find elements by class name using .
<h1 class="header">My Page.</h1>
<div class="other">
<p>other text</p>
</div>
$(".header");
$(".other");
jQuery supports most CSS3 selectors.
$("ul li");
$(".other p");
See the documentation for a full list
Sometimes a selection has more than you need.
Filter methods help refine your selection.
$("div").has("p");
$("li").first();
$("p").eq(2);
$("div").not("#important");
If you've made a selection you might want to use again, you can save it in a variable.
This is a good idea for performance.
$important = $("#important");
The convention is to start the variable name with a $ to indicate it contains a jQuery object.
jQuery has hundreds of actions that can be performed on any element
All the actions are methods
As methods they are called with dot notation
$(selector).action();
We use the attr
method as a getter and a setter
<a id="gdi-link" href="http://girldevelopit.com">
Girl Develop It
</a>
Get attribute
$("#gdi-link").attr('href');
Set attribute
var pghLink = 'http://girldevelopit.com/chapters/pittsburgh';
$("#gdi-link").attr('href', pghLink);
We use the html
method to
get and set an element's HTML
<div id="my-data">the data</div>
Get html
$("#my-data").html();
Set html
$("#my-data").html('different data!'); // Text
$("$my-data").html('<strong>Strong Data!</strong>'); // HTML
We use the text
method to
get and set an element's text
<div id="my-data">the data</div>
Get text
$("#my-data").text();
Set text
$("#my-data").text('different data!'); // Text
We use the append
and prepend
methods to
add to an element's HTML
<div id="my-data">the data</div>
Append - add to the end
$("#my-data").append('back');
$("#my-data").append('<strong>strong back</strong>');
Prepend - add to the beginning
$("#my-data").prepend('front');
$("#my-data").prepend('<strong>strong front</strong>');
var newDiv = $('<div></div>');
Use the remove
method to remove the element(s).
<div id="my-data">the data</div>
$('#my-data').remove();
We use the css
method as a getter and a setter
<div id="styled" styles="color: red;">styled text!</div>
Get style
$("#styled").css('color');
Set style
$("#styled").css('color', 'blue');
// Set multiple styles
$("#styled").css({
'margin': '10px',
'border': 'solid 1px black'
});
We use the
class manipulation
methods
to modify classes
$("#styled").addClass("active");
$("#styled").hasClass("active");
$("#styled").removeClass("active");
$("#styled").toggleClass("active");
Many jQuery methods return a jQuery object,
so you can call
additional methods
without pausing for a semicolon.
<div id="styled" styles="color: red;">styled text!</div>
Separately
$("#styled").css('color');
$("#styled").append('extra text!');
Chained
$("#styled").css('color').append('extra text!');
jQuery's each method makes it easy to iterate through elements.
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
$("li").each(function(index, element) {
$(element).text("This is item #" + index);
});
$()
with css selectors to find elements.Are you done or really stuck?
Check out our example.
The DOM takes time to load
Immediately running JS that depends on the DOM leads to problems
$(document).ready(function(){
// Code that depends on the DOM
});
$(function(){
// Code that depends on the DOM
});
$(document).ready(function(){
// Code that depends on the DOM
});
window.onload
with $(document).ready
Are you done or really stuck?
Check out our example.
Events occur on a webpage via user interaction
Common Events:
click
-
mouse clicks an element
mouseenter
-
mouse goes inside an element
mouseleave
-
mouse leaves an element
$(selector).mouseenter(function(){
//code when the mouse enters
});
$('.box').mouseenter(function(){
$(this).css('background-color', 'purple')
});
$('.box').mouseenter(function(){
$(this).css('background-color', 'purple')
});
$('.box').mouseleave(function(){
$(this).css('background-color', 'orange')
});
$('.box').click(function(){
$(this).css('background-color', 'green')
});
If you want multiple events to happen on the same element, you
should use the on
method
$('.box').on({
click: function() {
$(this).css('background-color', 'green')
},
mouseenter: function() {
$(this).css('background-color', 'purple')
},
mouseleave: function(){
$(this).css('background-color', 'orange')
}
});
document.ready
Are you done or really stuck?
Check out our example.
HTML Forms allow users to enter information
<form id="about-me">
<input type="text" id="name" placeholder="Enter a name"/>
<label>Do you like popcorn</label>
Yes <input type= "radio" name="popcorn" val="yes"/>
No <input type= "radio" name="popcorn" val="no"/>
<label>Favorite Dinosaur</label>
<select id="dinosaur">
<option value="t-rex">Tyrannosaurus Rex</option>
<option value="tri">Triceratops</option>
<option value="stego">Stegosaurus</option>
<option value="other">Other</option>
</select>
<input type="submit" value="Go!" style="padding: 7px; font-size:1em;"/>
</form>
HTML Forms allow users to enter information
You can use val to get values from a form
$('#name').val();
$('select#dinosaur').val();
$('input:radio[name=popcorn]:checked').val();
$('#name').val('Mitch');
$('select#dinosaur').val('stego');
$('input:radio[name=popcorn]:checked').val('no');
jQuery has an event for form submission
$('#about-me').submit(function(event){
//code to execute on submission
return false;
});
"return false" to prevent the form trying
return false;
to prevent the form from
submitting normallyHere's an example form.
<form id="activitiesForm">
<label for="activityName">Activity:</label>
<input type="text" id="activityName" />
<label for="timesPerMonth">Times a month:</label>
<input type="text" id="timesPerMonth" />
<input type="submit" value="Calculate" />
</form>
Are you done or really stuck?
Check out our example.