Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Basic Syntax Is: $ (Selector) .Action : $ (Document) .Ready (Function (// Jquery Methods Go Here... ) )

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

The jQuery syntax is tailor made for selecting HTML elements and performing

some action on the element(s).

Basic syntax is: $(selector).action()

 A $ sign to define/access jQuery


 A (selector) to "query (or find)" HTML elements
 A jQuery action() to be performed on the element(s)

Examples:

$(this).hide() - hides the current element.

$("p").hide() - hides all <p> elements.

$(".test").hide() - hides all elements with class="test".

$("#test").hide() - hides the element with id="test".

$(document).ready(function(){

// jQuery methods go here...

});
This is to prevent any jQuery code from running before the document is finished
loading (is ready).
Syntax Description

$("*") Selects all elements

$(this) Selects the current HTML element

$("p.intro") Selects all <p> elements with class="intro"

$("p:first") Selects the first <p> element

$("ul li:first") Selects the first <li> element of the first <ul>

$("ul li:first-child") Selects the first <li> element of every <ul>

$("[href]") Selects all elements with an href attribute

$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to "_blank"

$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal to "_blank"

$(":button") Selects all <button> elements and <input> elements of type="button"


$("tr:even") Selects all even <tr> elements

$("tr:odd") Selects all odd <tr> elements

Mouse Events Keyboard Events Form Events Document/Window Events

click keypress submit load

dblclick keydown change resize

mouseenter keyup focus scroll

mouseleave blur unload

$("p").click();
Ex-

<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></scr
ipt>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>

<p>If you click on me, I will disappear.</p>


<p>Click me away!</p>
<p>Click me too!</p>

</body>
</html>

Ex-2
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").focus(function(){
$(this).css("background-color", "cyan");
});
$("input").blur(function(){
$(this).css("background-color", "yellow");
});

$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});
});

$(selector).hide(speed,callback);

$(selector).show(speed,callback);

With jQuery, you can toggle between the hide() and show() methods with the
toggle() method.

$(selector).toggle(speed,callback);
$(selector).fadeIn(speed,callback);
$(selector).fadeOut(speed,callback);
$(selector).fadeToggle(speed,callback);

The jQuery fadeTo() method allows fading to a given opacity (value between 0
and 1).

$(selector).fadeTo(speed,opacity,callback);
$("button").click(function(){
$("#div1").fadeTo("slow", 0.15);
$("#div2").fadeTo("slow", 0.4);
$("#div3").fadeTo("slow", 0.7);
});
$(selector).slideDown(speed,callback);
$(selector).slideUp(speed,callback);
$(selector).slideToggle(speed,callback);
Ex- <!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></scr
ipt>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
</script>

<style>
#panel, #flip {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}

#panel {
padding: 50px;
display: block;
}
</style>
</head>
<body>

<div id="flip">Click to slide down panel</div>


<div id="panel">Hello world!</div>

</body>
</html>

The jQuery animate() method is used to create custom animations.

The optional callback parameter is a function to be executed after the animation


completes

$(selector).animate({params},speed,callback);

$("div").animate({left: '250px'});

The jQuery stop() method is used to stop an animation or effect before it is


finished.

The stop() method works for all jQuery effect functions, including sliding, fading
and custom animations.

$(selector).stop(stopAll,goToEnd);

Ex- $("#stop").click(function(){
$("#panel").stop();
});

Practise Example-

<!DOCTYPE html>

<html>

<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js
"></script>

<script>

$(document).ready(function(){

$("p").dblclick(function(){

$(this).hide();});

$("input").focus(function(){

$(this).css("background-color", "#cccccc");

});

$("input").blur(function(){

$(this).css("background-color", "#ffffff");

});

});

$(document).ready(function(){

$(".d").on({mouseenter:function(){

$(this).css("background","lightgray");},

mouseleave:function(){

$(this).css("background","lightblue");},

click: function(){
$(this).css("background-color", "yellow");

});

});

$(document).ready(function(){

$("#hideb").click(function(){

$(".hide").hide(function(){

alert("The paragraph is now hidden");

});});

$("#showb").click(function(){

$(".hide").show();});

$("#hideb1").click(function(){

$(".hide").hide(1000);});

$("#showb1").click(function(){

$(".hide").show(1000);});

});

</script>

</head>

<body>
<p>If you click on me, I will disappear.</p>

<p>Click me away!</p>

<p>Click me too!</p>

Name:<input type="text" name="fullname"><br>

Email:<input type="text" name="mail">

<br/>

<hr>

<div class="d">This is only check for on method</div>

<hr>

<div class="hide">this is hide line</div>

<hr>

<div id="hide">this is hide but slow</div>

<hr>

<button id ="hideb">Hidebutton</button>

<button id="showb">Showbut</button>

<button id ="hideb1">Hidebutton1</button>

<button id="showb1">Showbut1</button>

</body>

</html>
Add New HTML Content
We will look at four jQuery methods that are used to add new content:

 append() - Inserts content at the end of the selected elements


 prepend() - Inserts content at the beginning of the selected elements
 after() - Inserts content after the selected elements
 before() - Inserts content before the selected elements

Remove Elements/Content
To remove elements and content, there are mainly two jQuery methods:

 remove() - Removes the selected element (and its child elements)


 empty() - Removes the child elements from the selected element

jQuery Manipulating CSS


jQuery has several methods for CSS manipulation. We will look at the following
methods:

 addClass() - Adds one or more classes to the selected elements


 removeClass() - Removes one or more classes from the selected
elements
 toggleClass() - Toggles between adding/removing classes from the
selected elements
 css() - Sets or returns the style attribute

jQuery filter() Method


The filter() method lets you specify a criteria. Elements that do not match the
criteria are removed from the selection, and those that match will be returned.

The following example returns all <p> elements with class name "intro":
Example
$(document).ready(function(){
$("p").filter(".intro");
});

jQuery not() Method


The not() method returns all elements that do not match the criteria.

Tip: The not() method is the opposite of filter().

The following example returns all <p> elements that do not have class name
"intro":

Example
$(document).ready(function(){
$("p").not(".intro");
});

jQuery Effect Methods


The following table lists all the jQuery methods for creating animation effects.

Method Description

animate() Runs a custom animation on the selected elements

clearQueue() Removes all remaining queued functions from the selected elements

delay() Sets a delay for all queued functions on the selected elements
dequeue() Removes the next function from the queue, and then executes the function

fadeIn() Fades in the selected elements

fadeOut() Fades out the selected elements

fadeTo() Fades in/out the selected elements to a given opacity

fadeToggle() Toggles between the fadeIn() and fadeOut() methods

finish() Stops, removes and completes all queued animations for the selected elements

hide() Hides the selected elements

queue() Shows the queued functions on the selected elements

show() Shows the selected elements

slideDown() Slides-down (shows) the selected elements

slideToggle() Toggles between the slideUp() and slideDown() methods

slideUp() Slides-up (hides) the selected elements


stop() Stops the currently running animation for the selected elements

toggle() Toggles between the hide() and show() methods

jQuery Event Methods


Event methods trigger or attach a function to an event handler for the selected elements.

The following table lists all the jQuery methods used to handle events.

Method / Property Description

bind() Attaches event handlers to elements

blur() Attaches/Triggers the blur event

change() Attaches/Triggers the change event

click() Attaches/Triggers the click event

dblclick() Attaches/Triggers the double click event

delegate() Attaches a handler to current, or future, specified child elements of the matching elements

die() Removed in version 1.9. Removes all event handlers added with the live() method
error() Deprecated in version 1.8. Attaches/Triggers the error event

event.currentTarget The current DOM element within the event bubbling phase

event.data Contains the optional data passed to an event method when the current executing handler is bound

event.delegateTarget Returns the element where the currently-called jQuery event handler was attached

event.isDefaultPrevent Returns whether event.preventDefault() was called for the event object
ed()

event.isImmediateProp Returns whether event.stopImmediatePropagation() was called for the event object
agationStopped()

event.isPropagationSto Returns whether event.stopPropagation() was called for the event object
pped()

event.namespace Returns the namespace specified when the event was triggered

event.pageX Returns the mouse position relative to the left edge of the document

event.pageY Returns the mouse position relative to the top edge of the document

event.preventDefault() Prevents the default action of the event


event.relatedTarget Returns which element being entered or exited on mouse movement.

event.result Contains the last/previous value returned by an event handler triggered by the specified event

event.stopImmediateP Prevents other event handlers from being called


ropagation()

event.stopPropagation Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notifie
()

event.target Returns which DOM element triggered the event

event.timeStamp Returns the number of milliseconds since January 1, 1970, when the event is triggered

event.type Returns which event type was triggered

event.which Returns which keyboard key or mouse button was pressed for the event

focus() Attaches/Triggers the focus event

focusin() Attaches an event handler to the focusin event

focusout() Attaches an event handler to the focusout event


hover() Attaches two event handlers to the hover event

keydown() Attaches/Triggers the keydown event

keypress() Attaches/Triggers the keypress event

keyup() Attaches/Triggers the keyup event

live() Removed in version 1.9. Adds one or more event handlers to current, or future, selected elements

load() Deprecated in version 1.8. Attaches an event handler to the load event

mousedown() Attaches/Triggers the mousedown event

mouseenter() Attaches/Triggers the mouseenter event

mouseleave() Attaches/Triggers the mouseleave event

mousemove() Attaches/Triggers the mousemove event

mouseout() Attaches/Triggers the mouseout event

mouseover() Attaches/Triggers the mouseover event


mouseup() Attaches/Triggers the mouseup event

off() Removes event handlers attached with the on() method

on() Attaches event handlers to elements

one() Adds one or more event handlers to selected elements. This handler can only be triggered once per ele

$.proxy() Takes an existing function and returns a new one with a particular context

ready() Specifies a function to execute when the DOM is fully loaded

resize() Attaches/Triggers the resize event

scroll() Attaches/Triggers the scroll event

select() Attaches/Triggers the select event

submit() Attaches/Triggers the submit event

toggle() Removed in version 1.9. Attaches two or more functions to toggle between for the click event

trigger() Triggers all events bound to the selected elements


triggerHandler() Triggers all functions bound to a specified event for the selected elements

unbind() Removes an added event handler from selected elements

undelegate() Removes an event handler to selected elements, now or in the future

unload() Deprecated in version 1.8. Attaches an event handler to the unload event

You might also like