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:
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js">
</script>
</head>
Remember document.getElementById() and documet.getElementsByTagName()?
jQuery selectors let you get elements by:
var divs = $("div"); // All divs on page
var img = $("#mainpicture"); //img with id mainpicture
var images = $(".picture"); //All images with class picture
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();
<img id="mainpicture" src="http://girldevelopit.com/assets/pink-logo.png">
var img = $('#mainpicture');
img.attr('src');
img.attr('src', 'http://girldevelopit.com/assets/pink-logo.png');
var img = $('#mainpicture');
img.css('width');
img.css('width', '200px');
<div id = "results">Boo!</div>
var div = $('#results');
div.html();
div.html('New html!');
<div id = "results">Boo!</div>
var div = $('#results');
div.append('Additional html');
var div = $('#results');
div.prepend('Additional html (on top)');
var newDiv = $('<div></div>');
Try to convert last week's DOM interaction into jQuery.
Don't forget to include jQuery in your html head!
Webpages take time to load
Almost always, you don't want the JavaScript to be called until the page is loaded
$(document).ready(function(){
});
Events occur on a webpage via user interaction
Common Events:
$(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 bind method
$('.box').bind({
click: function() {
$(this).css('background-color', 'green')
},
mouseenter: function() {
$(this).css('background-color', 'purple')
},
mouseleave: function(){
$(this).css('background-color', 'orange')
}
});
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 JavaScript 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 after submission
return false;
});
"return false" to prevent the form trying to submit to a server.