Module 4.2
Module 4.2
//jQuery
$('#textbox').hide();
//jQuery
$('body').append( $("<h1/>").html("my text") ;
Enable jQuery in your page
• Introduce a jQuery function by using the below below
given function.
$(document).ready(function(){
//Script goes here
});
This is to prevent any jQuery code from running before the document
is finished loading (is ready).Here are some examples of actions that
can fail if methods are run before the document is fully loaded:
1.html
Selectors - Combined
• Syntax
$("tagName.className")
$("tagName.className#tagId")
• Examples
$("h1.mainTitle")
$("h1.mainTitle#firstHeading")
Index filters
•
Syntax: Examples:
$("selector:first") • $("p:first")
$("selector:last") • $("p:last")
• $("selector:odd") • $("p:odd")
• $("selector:even") • $("p:even")
• $("selector:eq(i)") • $("p:eq(1)")
• $("selector:gt(i)") • $("p:gt(1)")
• $("selector:lt(i)") • $("p:lt(1)")
• 2.html
Condition filters - Form filters
• $("selector:visible") • $("selector:input")
• $("selector:hidden") • $("selector:text")
• $("selector:password")
• $("selector:disabled")
• $("selector:enabled") • $("selector:radio")
• $("selector:checkbox")
• $("selector:checked")
• $("selector:selected") • $("selector:submit")
• $("selector:reset")
• $("selector:header") • $("selector:image")
• $("selector:animated") • $("selector:file")
• $("selector:button")
• 3.html
Relationship filters - Content filters
• $("selector:parent") •• $("selector:content('text')
• $("selector:first-child") ")
• $("selector:last-child") • $("selector:empty")
• $("selector:only-child") $("selector:has(selector)")
• $("selector:nth-child(i)")
• $("selector:nth-child(odd)")
• $("selector:nth-
child(even)")
Attribute filters
Syntax: Examples:
• $("selector[attribute]") • $("p:[name]")
• $("selector[attribute=value
]")
• $("p:[name=para]")
• $("selector[attribute!=value
]")
• $("p:[name!=para]")
• $("p:[name*=para]")
Set and Remove attributes
Syntax: Examples:
• $("selector").attr(properties) • $("img").attr({
"src" : "/path/",
"title" : "My Img"
• $("selector").removeAttr(attr) });
• $("div").removeAttr("class“)
• https://www.w3schools.com/jquery/jquery_ref_selectors.asp
Class, HTML, Text, Value - Functions
• $("selector").hasClass("className")
• $("selector").addClass("className")
• $("selector").removeClass("className")
• $("selector").toggleClass("className")
• $("selector").html()
• $("selector").html("html code")
• $("selector").text()
• $("selector").text("text content")
• $("selector").val()
• $("selector").val("value")
Examples
• $("selector").find("selector") • $("select").find("
• $("selector").each(function(){ option[value='india']")
$(this).xxxx(); • $("selector").each(function(){
}); $(this).addClass('title');
});
4.html
jQuery Event Methods
• $("selector").unbind(event, handler)
Bind - Example
$(function(){
$("#myButton").bind("onclick", validate);
//$("#myButton").click(validate);
});
function validate(){
if( $("#myText").val().length == 0 ) {
alert("Error")
} else {
$("#myForm").submit();
}
}
Animations
• show()
• hide()
• fadeIn()
• fadeOut()
• slideUp()
• slideDown()
• 6.html
jQuery Effects - Animation
• The jQuery animate() method is used to create
custom animations.
• Syntax:
• $(selector).animate({params},speed,callback);
• The required params parameter defines the
CSS properties to be animated.
• The optional speed parameter specifies the
duration of the effect. It can take the following
values: "slow", "fast", or milliseconds.
• The optional callback parameter is a function
to be executed after the animation completes.
$("#block").animate({
width: "+=60px",
opacity: 0.4,
fontSize:"3px",
borderWidth: "10px"
}, 1500);
Chaining
• Most jQuery methods return another jQuery
object - usually one representing the same
collection.This means you can chain methods
together:
$('div.section').hide().addClass('gone');
$('h1').fadeOut(1000).slideDown()
7.html