SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Jquery 2
Manish Singh
and
Hussain Fakhruddin
Agenda
 jQuery API- Core
 Selector and Context
 Data Accessors
 jQuery API -Attribute
- HTML Attributes
- Text Attributes
Quick Recap
 jQuery is a javascript liabrary.
 Has better browser copatiability.
 Provides Rich Enduser Experience.
 Lots of inbuilt functions and plugins.
 All the code is written within $ sign.
jQuery API.. Core
 The backbone of jquery.
 Contains main jquery related functions.
 Also contains list of object and data
accessor functions.
 We will have a look at them from next
slide onwards.
jQuery Functions
 jQuery( expression , context)
 jQuery( html, ownerDocument)
 jQuery(elements)
 jQuery( callback)
 See Next Slide for datails.
jQuery( expression , context)
 This function accepts a string containing a
CSS selector which is then used to match
a set of elements.
 Arguments are an expression ( an
expression to search with, compulsory)
and context ( any DOM or jquery context,
optional).
 The core functionality of jQuery centers
around this function.
Continued…
 Everything in jQuery is based upon this, or
uses this in some way.
 The most basic use of this function is to
pass in an expression (usually consisting
of CSS), which then finds all matching
elements.
 By default, if no context is specified, $()
looks for DOM elements within the context
of the current HTML document.
Example
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd"> <html>
<head>
<script src="http://code.jquery.com/jquery-latest.js
"></script> <script>
$(document).ready(function(){
$("div > p").css("border", "1px solid gray"); });
</script> </head>
<body>
<p>one</p> <div><p>two</p></div>
<p>three</p>
</body> </html>
jQuery( html, [ownerDocument]
)
Create DOM elements on-the-fly from the
provided String of raw HTML.
Simple elements without attributes, e.g.,
"<div />", are created via
document.createElement.
All other cases are parsed by assigning the
string to the .innerHTML property of a div
element.
The HTML string cannot contain elements
that are invalid within a div, such as html,
head, body, or title elements.
Continued…
Arguments are an html (A string of HTML
to create on the fly - compulsory) and
ownerDocument (A document in which the
new elements will be created - optional).
All HTML must be well-formed, otherwise
it may not work correctly across all
browsers.
Example :- $
("<div><p>Hello</p></div>").appendTo
("body") .
jQuery( elements )
Wrap jQuery functionality around a single
or multiple DOM Element(s).
This function accepts XML Documents and
Window objects as valid arguments even
though they are not DOM Elements.
Argument is element (DOM element(s) to
be encapsulated by a jQuery object-
compulsory).
Example :- $
(document.body).css( "background",
"black" );
jQuery( callback )
A shorthand for $(document).ready().
Argument is callback (The function to
execute when the DOM is ready -
Compulsory).
Allows you to bind a function to be
executed when the DOM document has
finished loading.
$(function(){
// Document is ready
});
Object Accesors
 Each()
 Size()
 Length
 Selector()
 Context()
 Eq()
 Get()
 Index()
each( callback )
Execute a function within the context of
every matched element.
Argument is callback(The callback to
execute for each matched element-
compulsory).
This means that every time the passed-in
function is executed (which is once for
every element matched) the 'this'
keyword points to the specific DOM
element.
Example
<html> <head>
<script src="http://code.jquery.com/jquery-latest.js
"></script> <script type=“text/javascript”>
$(document).ready(function(){
$(document.body).click(function () {
$("div").each(function (i) {
if (this.style.color != "blue") {
this.style.color = "blue";
} else {
this.style.color = "";
} }); }); }); </script>
<style>
div { color:red; text-align:center;
cursor:pointer; font-weight:bolder;
width:300px; }
</style>
</head> <body>
<div>Click here</div> <div>to iterate
through</div> <div>these divs.</div>
</body> </html>
Size() and length
The number of elements in the jQuery object.
Size() is slightly slower. So length should be
used.
Example :- <html> <head>
<script src="http://code.jquery.com/jquery-latest.js
"></script> <script>
$(document).ready(function(){
$(document.body).click(function () { $
(document.body).append($("<div>")); var n = $
("div").length; $("span").text("There are " + n + "
divs." + "Click to add more."); }).trigger('click'); //
trigger the click to start }); </script>
Continued
<style>
body { cursor:pointer; }
div { width:50px; height:30px; margin:5px;
float:left; background:green; }
span { color:red; } </style>
</head> <body>
<span></span>
<div></div>
</body> </html>
Selector and Context
 New addition in jQuery 1.3.
 A selector representing selector originally
passed to jQuery().
 Context represents the DOM node context
originally passed to jQuery() (if none was
passed then context will be equal to the
document).
 Useful for plugin developers.
 We will see in the detail later.
Continued…
eq( position ) :- Reduce the set of matched
elements to a single element.
Example :- $("p").eq(1).css("color", "red") .
get( ) :- Access all matched DOM elements.
Example :- function disp(divs) {
var a = [];
for (var i = 0; i < divs.length; i++) {
a.push(divs[i].innerHTML); } $("span").text(a.join("
")); }
disp( $("div").get().reverse() );
Continued…
 get( index ) :- Access a single matched
DOM element at a specified index in the
matched set.
 Example :- $("*",
document.body).click(function (e)
{ e.stopPropagation();
var domEl = $(this).get(0);
$("span:first").text("Clicked on - " +
domEl.tagName); });
Continued…
index( subject ) :- Searches every matched
element for the object and returns the index
of the element.
If a jQuery object is passed, only the first
element is checked.
Returns -1 if the object wasn't found.
Example :- $("div").click(function () {
// this is the dom element clicked
var index = $("div").index(this);
$("span").text("That was div index #" + index); });
Data Accessors
 data( name ) :- Returns value at named
data store for the element, as set by
data(name, value).
 data( name, value ) :- Stores the value
in the named spot.
 removeData( name ) :- Removes named
data store from an element.
Example
<html> <head>
<script src="
http://code.jquery.com/jquery-latest.js
"></script> <script> $
(document).ready(function(){
$("div").data("test", { first: 16, last: "pizza!" }); $
("span:first").text($("div").data("test").first); $
("span:last").text($("div").data("test").last); });
</script> <style> div { color:blue; }
span { color:red; } </style>
</head>
<body> <div> The values stored were
<span></span> and <span></span> </div>
jQuery API.. Attribute
 Attributes are the properties associated
with any HTML element.
 So attribute APIs deal with accessing any
attribute, getting and setting attribute
values and other related operations on
attributes.
attr( name )
 Access a property on the first matched
element.
 This method makes it easy to retrieve a
property value from the first matched
element.
 Attributes include title, alt, src, href,
width, style, etc.
Example
<html> <head>
<script src="http://code.jquery.com/jquery-latest.js
"></script> <script> $(document).ready(function(){
var title = $("em").attr("title");
$("div").text(title); }); </script>
<style> em { color:blue; font-weight;bold; }
div { color:red; } </style>
</head> <body> <p> Once there was a
<em title="huge, gigantic">large</em> dinosaur...
</p>
The title of the emphasis is:<div></div>
</body> </html>
attr( properties )
 Set a key/value object as properties to all
matched elements.
 This serves as the best way to set a large
number of properties on all matched
elements.
 Argument is properties i.e. a Key/value
pairs to set as object properties.
Example
<html> <head>
<script src="http://code.jquery.com/jquery-
latest.js"></script>
<script> $(document).ready(function(){
$("img").attr({ src: "/images/hat.gif",
title: "jQuery", alt: "jQuery Logo" }); $
("div").text($("img").attr("alt")); }); </script>
<style> img { padding:10px; }
div { color:red; font-size:24px; } </style>
</head> <body> <img /> <img /> <img />
<div><B>Attribute of Ajax</B></div>
</body> </html>
Other attr functions
 attr( key, value ) :- Set a single property
to a value, on all matched elements.
 attr( key, fn ) :- Set a single property to
a computed value, on all matched
elements.
 In second type instead of supplying a
string value , a function is provided that
computes the value.
Example 1
<html> <head>
<script src="http://code.jquery.com/jquery-
latest.js"></script> <script> $
(document).ready(function(){ $
("button:gt(1)").attr("disabled","disabled"); });
</script> <style> button { margin:10px; }
</style>
</head> <body>
<button>0th Button</button>
<button>1st Button</button>
<button>2nd Button</button>
</body> </html>
Example 2
<html> <head>
<script src="http://code.jquery.com/jquery-
latest.js"></script> <script> $
(document).ready(function(){
$("div").attr("id", function (arr) { return "div-id" + arr; })
.each(function () {
$("span", this).html("(ID = '<b>" + this.id +
"</b>')"); }); }); </script>
<style> div { color:blue; } span { color:red; }
b { font-weight:bolder; } </style>
</head> <body> <div>Zero-th <span></span></div>
<div>First <span></span></div>
<div>Second <span></span></div> </body> </html>
removeAttr( name )
 Remove an attribute from each of the
matched elements.
 Takes name of the attribute as parameter.
Example
<html> <head>
<script src="http://code.jquery.com/jquery-
latest.js"></script> <script> $
(document).ready(function(){ $
("button").click(function () { $
(this).next().removeAttr("disabled") .focus()
.val("editable now"); }); }); </script>
</head> <body>
<button>Enable</button> <input type="text"
disabled="disabled" value="can't edit this" />
</body> </html>
HTML Attributes
 html( ) :- Get the html contents
(innerHTML) of the first matched element.
 html( val ) :- Set the html contents of
every matched element.
 Above properties will not work for XML
documents ( though it will work for
XHTML).
Example
<html> <head>
<script src="http://code.jquery.com/jquery-
latest.js"></script> <script> $
(document).ready(function(){
$("p").click(function () {
var htmlStr = $(this).html(); $
(this).text(htmlStr); }); }); </script>
<style> p { margin:8px; font-size:20px;
color:blue; cursor:pointer; }
b { text-decoration:underline; }
button { cursor:pointer; } </style>
</head >
<body> <p>
<b>Click</b> to change the <span
id="tag">html</span> </p>
<p> to a <span id="text">text</span> node.
</p>
<p> This <button
name="nada">button</button> does nothing.
</p>
</body>
</html>
Example 2
<html> <head>
<script src="http://code.jquery.com/jquery-
latest.js"></script> <script> $
(document).ready(function(){
$("div").html("<span class='red'>Hello
<b>Again</b></span>"); }); </script>
<style> .red { color:red; } </style>
</head> <body> <span>Hello</span>
<div></div>
<div></div>
<div></div>
</body> </html>
Text Attributes
 text() :- Get the combined text contents
of all matched elements.
 text( val ) :- Set the text contents of all
matched elements.
 Above two work on both HTML and XML
documents.
 Cannot be used on input elements. For
input field text use the val attribute.
Continued…
 val( ) :- Get the input value of the first
matched element.
 val( val ) :- Set the value attribute of
every matched element.
 Val(val) also checks, or selects, all the
radio buttons, checkboxes, and select
options that match the set of values.
Example
<html> <head>
<script src="http://code.jquery.com/jquery-
latest.js"></script> <script> $
(document).ready(function(){ $
("#single").val("Single2"); $
("#multiple").val(["Multiple2", "Multiple3"]);
$("input").val(["check1","check2",
"radio1" ]); }); </script> <style> body
{ color:blue; } </style>
</head> <body> <select id="single">
<option>Single</option>
<option>Single2</option> </select>
<select id="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select> <br/> <input type="checkbox"
name="checkboxname" value="check1"/> check1
<input type="checkbox" name="checkboxname"
value="check2"/> check2
<input type="radio" name="r" value="radio1"/>
radio1
<input type="radio" name="r" value="radio2"/>
radio2
</body> </html>
Thank You!!!
Email :
manish_singh185@yahoo.com
hussulinux@gmail.com

Weitere ähnliche Inhalte

Was ist angesagt?

Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial EnAnkur Dongre
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practicesmanugoel2003
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQueryAkshay Mathur
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 
Doctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperDoctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperJonathan Wage
 
Entities in drupal 7
Entities in drupal 7Entities in drupal 7
Entities in drupal 7Zsolt Tasnadi
 
ZendCon2010 Doctrine MongoDB ODM
ZendCon2010 Doctrine MongoDB ODMZendCon2010 Doctrine MongoDB ODM
ZendCon2010 Doctrine MongoDB ODMJonathan Wage
 
e-suap - client technologies- english version
e-suap - client technologies- english versione-suap - client technologies- english version
e-suap - client technologies- english versionSabino Labarile
 
Symfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODMSymfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODMJonathan Wage
 
Semantic code transformations in MetaJS
Semantic code transformations in MetaJSSemantic code transformations in MetaJS
Semantic code transformations in MetaJSDmytro Dogadailo
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comDrupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comJD Leonard
 
Entity Query API
Entity Query APIEntity Query API
Entity Query APImarcingy
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entitiesdrubb
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQueryorestJump
 

Was ist angesagt? (20)

J query training
J query trainingJ query training
J query training
 
Jquery
JqueryJquery
Jquery
 
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial EnEjb3 Struts Tutorial En
Ejb3 Struts Tutorial En
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
AngularJS
AngularJSAngularJS
AngularJS
 
Field api.From d7 to d8
Field api.From d7 to d8Field api.From d7 to d8
Field api.From d7 to d8
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Doctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperDoctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document Mapper
 
Entities in drupal 7
Entities in drupal 7Entities in drupal 7
Entities in drupal 7
 
ZendCon2010 Doctrine MongoDB ODM
ZendCon2010 Doctrine MongoDB ODMZendCon2010 Doctrine MongoDB ODM
ZendCon2010 Doctrine MongoDB ODM
 
e-suap - client technologies- english version
e-suap - client technologies- english versione-suap - client technologies- english version
e-suap - client technologies- english version
 
Symfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODMSymfony Day 2010 Doctrine MongoDB ODM
Symfony Day 2010 Doctrine MongoDB ODM
 
Semantic code transformations in MetaJS
Semantic code transformations in MetaJSSemantic code transformations in MetaJS
Semantic code transformations in MetaJS
 
Jquery 4
Jquery 4Jquery 4
Jquery 4
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comDrupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.com
 
Entity Query API
Entity Query APIEntity Query API
Entity Query API
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entities
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQuery
 

Ähnlich wie Jquery 2

Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQueryGunjan Kumar
 
jQuery - Tips And Tricks
jQuery - Tips And TricksjQuery - Tips And Tricks
jQuery - Tips And TricksLester Lievens
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012ghnash
 
A Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NETA Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NETJames Johnson
 
J Query(04 12 2008) Foiaz
J Query(04 12 2008) FoiazJ Query(04 12 2008) Foiaz
J Query(04 12 2008) Foiaztestingphase
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETJames Johnson
 
Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Rakesh Jha
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentationMevin Mohan
 
Jquery tutorial
Jquery tutorialJquery tutorial
Jquery tutorialBui Kiet
 

Ähnlich wie Jquery 2 (20)

jQuery
jQueryjQuery
jQuery
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
jQuery
jQueryjQuery
jQuery
 
J Query Public
J Query PublicJ Query Public
J Query Public
 
jQuery Beginner
jQuery BeginnerjQuery Beginner
jQuery Beginner
 
jQuery - Tips And Tricks
jQuery - Tips And TricksjQuery - Tips And Tricks
jQuery - Tips And Tricks
 
jquery.ppt
jquery.pptjquery.ppt
jquery.ppt
 
Jquery
JqueryJquery
Jquery
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
jQuery Presentasion
jQuery PresentasionjQuery Presentasion
jQuery Presentasion
 
A Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NETA Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NET
 
J Query(04 12 2008) Foiaz
J Query(04 12 2008) FoiazJ Query(04 12 2008) Foiaz
J Query(04 12 2008) Foiaz
 
jQuery
jQueryjQuery
jQuery
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NET
 
Java script
Java scriptJava script
Java script
 
Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
Jquery tutorial
Jquery tutorialJquery tutorial
Jquery tutorial
 
Dom
Dom Dom
Dom
 
ReactJS.ppt
ReactJS.pptReactJS.ppt
ReactJS.ppt
 

Kürzlich hochgeladen

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Kürzlich hochgeladen (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Jquery 2

  • 2. Agenda  jQuery API- Core  Selector and Context  Data Accessors  jQuery API -Attribute - HTML Attributes - Text Attributes
  • 3. Quick Recap  jQuery is a javascript liabrary.  Has better browser copatiability.  Provides Rich Enduser Experience.  Lots of inbuilt functions and plugins.  All the code is written within $ sign.
  • 4. jQuery API.. Core  The backbone of jquery.  Contains main jquery related functions.  Also contains list of object and data accessor functions.  We will have a look at them from next slide onwards.
  • 5. jQuery Functions  jQuery( expression , context)  jQuery( html, ownerDocument)  jQuery(elements)  jQuery( callback)  See Next Slide for datails.
  • 6. jQuery( expression , context)  This function accepts a string containing a CSS selector which is then used to match a set of elements.  Arguments are an expression ( an expression to search with, compulsory) and context ( any DOM or jquery context, optional).  The core functionality of jQuery centers around this function.
  • 7. Continued…  Everything in jQuery is based upon this, or uses this in some way.  The most basic use of this function is to pass in an expression (usually consisting of CSS), which then finds all matching elements.  By default, if no context is specified, $() looks for DOM elements within the context of the current HTML document.
  • 8. Example  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script src="http://code.jquery.com/jquery-latest.js "></script> <script> $(document).ready(function(){ $("div > p").css("border", "1px solid gray"); }); </script> </head> <body> <p>one</p> <div><p>two</p></div> <p>three</p> </body> </html>
  • 9. jQuery( html, [ownerDocument] ) Create DOM elements on-the-fly from the provided String of raw HTML. Simple elements without attributes, e.g., "<div />", are created via document.createElement. All other cases are parsed by assigning the string to the .innerHTML property of a div element. The HTML string cannot contain elements that are invalid within a div, such as html, head, body, or title elements.
  • 10. Continued… Arguments are an html (A string of HTML to create on the fly - compulsory) and ownerDocument (A document in which the new elements will be created - optional). All HTML must be well-formed, otherwise it may not work correctly across all browsers. Example :- $ ("<div><p>Hello</p></div>").appendTo ("body") .
  • 11. jQuery( elements ) Wrap jQuery functionality around a single or multiple DOM Element(s). This function accepts XML Documents and Window objects as valid arguments even though they are not DOM Elements. Argument is element (DOM element(s) to be encapsulated by a jQuery object- compulsory). Example :- $ (document.body).css( "background", "black" );
  • 12. jQuery( callback ) A shorthand for $(document).ready(). Argument is callback (The function to execute when the DOM is ready - Compulsory). Allows you to bind a function to be executed when the DOM document has finished loading. $(function(){ // Document is ready });
  • 13. Object Accesors  Each()  Size()  Length  Selector()  Context()  Eq()  Get()  Index()
  • 14. each( callback ) Execute a function within the context of every matched element. Argument is callback(The callback to execute for each matched element- compulsory). This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points to the specific DOM element.
  • 15. Example <html> <head> <script src="http://code.jquery.com/jquery-latest.js "></script> <script type=“text/javascript”> $(document).ready(function(){ $(document.body).click(function () { $("div").each(function (i) { if (this.style.color != "blue") { this.style.color = "blue"; } else { this.style.color = ""; } }); }); }); </script>
  • 16. <style> div { color:red; text-align:center; cursor:pointer; font-weight:bolder; width:300px; } </style> </head> <body> <div>Click here</div> <div>to iterate through</div> <div>these divs.</div> </body> </html>
  • 17. Size() and length The number of elements in the jQuery object. Size() is slightly slower. So length should be used. Example :- <html> <head> <script src="http://code.jquery.com/jquery-latest.js "></script> <script> $(document).ready(function(){ $(document.body).click(function () { $ (document.body).append($("<div>")); var n = $ ("div").length; $("span").text("There are " + n + " divs." + "Click to add more."); }).trigger('click'); // trigger the click to start }); </script>
  • 18. Continued <style> body { cursor:pointer; } div { width:50px; height:30px; margin:5px; float:left; background:green; } span { color:red; } </style> </head> <body> <span></span> <div></div> </body> </html>
  • 19. Selector and Context  New addition in jQuery 1.3.  A selector representing selector originally passed to jQuery().  Context represents the DOM node context originally passed to jQuery() (if none was passed then context will be equal to the document).  Useful for plugin developers.  We will see in the detail later.
  • 20. Continued… eq( position ) :- Reduce the set of matched elements to a single element. Example :- $("p").eq(1).css("color", "red") . get( ) :- Access all matched DOM elements. Example :- function disp(divs) { var a = []; for (var i = 0; i < divs.length; i++) { a.push(divs[i].innerHTML); } $("span").text(a.join(" ")); } disp( $("div").get().reverse() );
  • 21. Continued…  get( index ) :- Access a single matched DOM element at a specified index in the matched set.  Example :- $("*", document.body).click(function (e) { e.stopPropagation(); var domEl = $(this).get(0); $("span:first").text("Clicked on - " + domEl.tagName); });
  • 22. Continued… index( subject ) :- Searches every matched element for the object and returns the index of the element. If a jQuery object is passed, only the first element is checked. Returns -1 if the object wasn't found. Example :- $("div").click(function () { // this is the dom element clicked var index = $("div").index(this); $("span").text("That was div index #" + index); });
  • 23. Data Accessors  data( name ) :- Returns value at named data store for the element, as set by data(name, value).  data( name, value ) :- Stores the value in the named spot.  removeData( name ) :- Removes named data store from an element.
  • 24. Example <html> <head> <script src=" http://code.jquery.com/jquery-latest.js "></script> <script> $ (document).ready(function(){ $("div").data("test", { first: 16, last: "pizza!" }); $ ("span:first").text($("div").data("test").first); $ ("span:last").text($("div").data("test").last); }); </script> <style> div { color:blue; } span { color:red; } </style> </head> <body> <div> The values stored were <span></span> and <span></span> </div>
  • 25. jQuery API.. Attribute  Attributes are the properties associated with any HTML element.  So attribute APIs deal with accessing any attribute, getting and setting attribute values and other related operations on attributes.
  • 26. attr( name )  Access a property on the first matched element.  This method makes it easy to retrieve a property value from the first matched element.  Attributes include title, alt, src, href, width, style, etc.
  • 27. Example <html> <head> <script src="http://code.jquery.com/jquery-latest.js "></script> <script> $(document).ready(function(){ var title = $("em").attr("title"); $("div").text(title); }); </script> <style> em { color:blue; font-weight;bold; } div { color:red; } </style> </head> <body> <p> Once there was a <em title="huge, gigantic">large</em> dinosaur... </p> The title of the emphasis is:<div></div> </body> </html>
  • 28. attr( properties )  Set a key/value object as properties to all matched elements.  This serves as the best way to set a large number of properties on all matched elements.  Argument is properties i.e. a Key/value pairs to set as object properties.
  • 29. Example <html> <head> <script src="http://code.jquery.com/jquery- latest.js"></script> <script> $(document).ready(function(){ $("img").attr({ src: "/images/hat.gif", title: "jQuery", alt: "jQuery Logo" }); $ ("div").text($("img").attr("alt")); }); </script> <style> img { padding:10px; } div { color:red; font-size:24px; } </style> </head> <body> <img /> <img /> <img /> <div><B>Attribute of Ajax</B></div> </body> </html>
  • 30. Other attr functions  attr( key, value ) :- Set a single property to a value, on all matched elements.  attr( key, fn ) :- Set a single property to a computed value, on all matched elements.  In second type instead of supplying a string value , a function is provided that computes the value.
  • 31. Example 1 <html> <head> <script src="http://code.jquery.com/jquery- latest.js"></script> <script> $ (document).ready(function(){ $ ("button:gt(1)").attr("disabled","disabled"); }); </script> <style> button { margin:10px; } </style> </head> <body> <button>0th Button</button> <button>1st Button</button> <button>2nd Button</button> </body> </html>
  • 32. Example 2 <html> <head> <script src="http://code.jquery.com/jquery- latest.js"></script> <script> $ (document).ready(function(){ $("div").attr("id", function (arr) { return "div-id" + arr; }) .each(function () { $("span", this).html("(ID = '<b>" + this.id + "</b>')"); }); }); </script> <style> div { color:blue; } span { color:red; } b { font-weight:bolder; } </style> </head> <body> <div>Zero-th <span></span></div> <div>First <span></span></div> <div>Second <span></span></div> </body> </html>
  • 33. removeAttr( name )  Remove an attribute from each of the matched elements.  Takes name of the attribute as parameter.
  • 34. Example <html> <head> <script src="http://code.jquery.com/jquery- latest.js"></script> <script> $ (document).ready(function(){ $ ("button").click(function () { $ (this).next().removeAttr("disabled") .focus() .val("editable now"); }); }); </script> </head> <body> <button>Enable</button> <input type="text" disabled="disabled" value="can't edit this" /> </body> </html>
  • 35. HTML Attributes  html( ) :- Get the html contents (innerHTML) of the first matched element.  html( val ) :- Set the html contents of every matched element.  Above properties will not work for XML documents ( though it will work for XHTML).
  • 36. Example <html> <head> <script src="http://code.jquery.com/jquery- latest.js"></script> <script> $ (document).ready(function(){ $("p").click(function () { var htmlStr = $(this).html(); $ (this).text(htmlStr); }); }); </script> <style> p { margin:8px; font-size:20px; color:blue; cursor:pointer; } b { text-decoration:underline; } button { cursor:pointer; } </style> </head >
  • 37. <body> <p> <b>Click</b> to change the <span id="tag">html</span> </p> <p> to a <span id="text">text</span> node. </p> <p> This <button name="nada">button</button> does nothing. </p> </body> </html>
  • 38. Example 2 <html> <head> <script src="http://code.jquery.com/jquery- latest.js"></script> <script> $ (document).ready(function(){ $("div").html("<span class='red'>Hello <b>Again</b></span>"); }); </script> <style> .red { color:red; } </style> </head> <body> <span>Hello</span> <div></div> <div></div> <div></div> </body> </html>
  • 39. Text Attributes  text() :- Get the combined text contents of all matched elements.  text( val ) :- Set the text contents of all matched elements.  Above two work on both HTML and XML documents.  Cannot be used on input elements. For input field text use the val attribute.
  • 40. Continued…  val( ) :- Get the input value of the first matched element.  val( val ) :- Set the value attribute of every matched element.  Val(val) also checks, or selects, all the radio buttons, checkboxes, and select options that match the set of values.
  • 41. Example <html> <head> <script src="http://code.jquery.com/jquery- latest.js"></script> <script> $ (document).ready(function(){ $ ("#single").val("Single2"); $ ("#multiple").val(["Multiple2", "Multiple3"]); $("input").val(["check1","check2", "radio1" ]); }); </script> <style> body { color:blue; } </style> </head> <body> <select id="single"> <option>Single</option> <option>Single2</option> </select>
  • 42. <select id="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select> <br/> <input type="checkbox" name="checkboxname" value="check1"/> check1 <input type="checkbox" name="checkboxname" value="check2"/> check2 <input type="radio" name="r" value="radio1"/> radio1 <input type="radio" name="r" value="radio2"/> radio2 </body> </html>