J Query
J Query
What is jQuery?
A library of JavaScript functions
Features Select and manipulate HTML Manipulate CSS JavaScript Effects and animations HTML DOM traversal and modification AJAX Utilities
Getting Started
<!DOCTYPE html> <html> <head> <title>Fun with jQuery</title> </head>
source file Define jQuery functions Save this file as index.htm in a 216/jQuery subdirectory on ned Try it!
<body> <h2>Hello, jQuery!</h2> <button id='btnOuch'>Say Ouch</button> <script src="http://code.jquery.com/jquery.js"></script> <script> $("#btnOuch").click(function(){ alert("Ouch! That hurt."); }); </script> </body> </html>
btnOuch click() binds a click event to selected element The function executes when the click event is fired
Display an alert when the button with ID btnOuch is clicked
http://www.w3schools.com/jquery/jquery_syntax.asp
jQuery Selectors
Syntax $(this) $("p") $("p.intro") $(".intro") $("#intro") $("ul li:first") $("[href$='.jpg']") Description Current HTML element All <p> elements All <p> elements with class="intro" All elements with class="intro" The first element with id="intro" The first <li> element of each <ul> All elements with an href attribute that ends with ".jpg" All elements with class="head" inside a <div> element with id="intro"
$("div#intro .head")
Comparison
Compare the following:
What are the advantages of the jQuery method?
Example 2
<script> $("h2").click(function(){ $(this).hide("slow"); }); </script>
What will this do? What happens if you have more than one h2? Try it!
Example 3
Hide all blue paragraphs when btnHideBlue is clicked
jQuery Events
Event Method Description
$(selector).click(function)
$(selector).dblclick(function) $(selector).focus(function) $(selector).mouseover(function) $(selector).keypress(function)
For a full jQuery event reference, please see jQuery Events Reference
Example 4
Append text to paragraph lemon on mouseover
Manipulating CSS
CSS Properties $(selector).css(propertyName) $(selector).css(propertyName,value) Description Get the style property value of the first selected element Set the value of one style property for selected elements
$(selector).css({properties})
$(selector).addClass(class)
For a full jQuery CSS reference, please see jQuery CSS Methods Reference For more on the css function, see http://api.jquery.com/css/
Example 5
Example 6
<script> $("#btnColorCheck").click(function(){ alert($("#lemon").css("color")); What color is the paragraph? }); </script>
Example 7
Highlight (background-color = yellow) any paragraph that is double-clicked
jQuery Effects
Function $(selector).hide() $(selector).show() Description Hide selected elements Show selected elements
$(selector).toggle() $(selector).slideDown()
$(selector).slideUp()
Toggle (between hide and show) selected elements Slide-down (show) selected elements
Slide-up (hide) selected elements
$(selector).slideToggle() Toggle slide-up and slide-down of selected elements $(selector).fadeIn() $(selector).fadeOut() $(selector).fadeTo() Fade in selected elements Fade out selected elements Fade out selected elements to a given opacity
$(selector).fadeToggle()
Example 8
Create a toggle button that shows/hides paragraph lemon.
Example 9
Manipulating HTML
Function $(selector).html(content) $(selector).append(content) $(selector).after(content) Description Changes the (inner) HTML of selected elements Appends content to the (inner) HTML of selected elements Adds HTML after selected elements
For a full jQuery HTML reference, please see jQuery HTML Methods Reference
Example 10
<script> $("#btnReplace").click(function(){ $("#lemon").html("Lollipop souffl ice cream tootsie roll donut..."); }); </script>
Replace text in paragraph lemon when btnReplace is clicked.
Ajax
The jQuery $.post() method loads data from the server using
function(data) Optional. Specifies a function to run if the request succeeds. data - contains the resulting data from the request
http://www.w3schools.com/jquery/ajax_post.asp
Ajax
show_product.php <?php
$id = $_POST['id'];
mysql_connect("localhost", "omuser", "omuser") or die("Error connecting"); mysql_select_db("om") or die("Error selecting DB"); $query = "SELECT * FROM items WHERE item_id = $id"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { echo "Product ID $id not found."; return; } $row = mysql_fetch_array($result); echo "<strong>Item ID:</strong> {$row['item_id']}<br>"; echo "<strong>Title:</strong> {$row['title']}<br>"; echo "<strong>Artist:</strong> {$row['artist']}<br>"; echo "<strong>Price:</strong> {$row['unit_price']}<br>";
Ajax
ajax.php $('#show').click(function(){ var prodId = $('#id').val(); $.post( "show_product.php", {id:prodId},
When the button is clicked Get the text box value Ajax POST Name of the PHP script
function(result) { $('#detail').html(result); }
); });
Update the "detail" div With the output of the PHP script
Resources
http://jquery.com/
http://www.w3schools.com/jquery
http://jqueryui.com/