SlideShare uma empresa Scribd logo
1 de 21
HTML DOM & JQuery
“Less is more”
Compiled from Wikipedia.org, W3Schools.com & JQuery.com
by Abdul Hafeez
HTML DOM & JQuery : Agenda
• DOM – Document Object Model
• DOM History
• Node Document Tree
• Find & Access Nodes
• What is JQuery
• Why JQuery
• JQuery Philosophy
• How to assign events using JQuery
• Asynchronous Dynamic Fun
DOCUMENT OBJECT MODEL
• Platform and language independent standard object model for representing HTML or
XML related formats
• Level 1 DOM specification is published in 1998 by W3C, and it allowed to and
manipulation of every single element.
• No cross browser incompatibility
• DOM is the way JavaScript sees its containing HTML page
• DOM supports navigation in any direction
• SAX (Simple API for XML) is a popular alternative to the DOM
• DOM is separated in different parts
– Core
– XML
– HTML
• And into different levels, Level 1/2/3
HTML DOM
• Defines a standard way for accessing and manipulating
HTML documents
• Everything in an HTML document is a node.
– Document Node : Entire Document
– Element Node : HTML tags
– Text Node : Texts in element node
– Attribute Node : HTML attribute
– Comment Nodes
• DOM presents an HTML document as a tree structure (a node tree) with elements,
attributes and texts.
The tree starts at the document node and continues to branch out until it has reached
all text nodes at the lowest level.
• All the nodes in a node tree have relationships to each other (e.g. parent/child).
HTML DOM
Contd….
• With JavaScript we can restructure an entire HTML
document. We can add, remove, change or reorder
items on a page.
• To do so, JavaScript needs access to all elements in the
HTML document, and this access to the elements is
given through DOM.
HTML DOM
Contd….
Node/Document Tree
• All nodes have relationships to each other
• Except document node, every other node has a parent node
• Most element nodes have child nodes
• Nodes might share a parent and we call them siblings
• Nodes can also have descendants, meaning all the nodes that are children of a node,
or children of those children and so on.
• Nodes can also have ancestors. Ancestors are nodes that are parents of a node, or
parents of this parent and so on.
Find & Access Nodes
• JavaScript provides methods and properties to find and access nodes
• Using getElementById() and getElementByTagName() mothods
– It doesn’t follow document structure
– Can find any HTML element in the entire document.
– Gives us the HTML element(s) we need regardless of where they are in the document
– getElementByTagName(‘p’) gives us all the <p> elements in the document
– getElementById(‘RT’) returns the correct element whether it is hidden in the tree
• Using parentNode, firstChild and lastChild properties of an element node
– Follows document structure and allow short-distance travel in the document
What is jQuery
• An open source JavaScript library that simplifies the
interaction between HTML and JavaScript
• Is designed to change the way you write JavaScript
Why jQuery?
• Fully documented
• Great community
• Tons of plugins
• Small size (15kb)
• Everything works in IE 6+, Firefox, Safari 2+ and Opera
9+
• Simplifies how you handle events, perform animations or
add Ajax interactions
• And it is fast
• Can learn in 30 minutes
jQuery Philosophy
• (Almost) every operation boils down to:
– Find some stuff
– Do something to it
jQuery
• Select all <div> tags from the document
• document.getElementsByTagName(‘div’)
• jQuery(‘div’)
• $(‘div’)
• Select an element whose id is ‘DX’
• document.getElementById(‘DX’)
• $(‘#DX’)
• $(‘#DX’).hide(‘slow’)
jQuery
• Add a class (css) to all links in a document
• $(‘a’).addClass(‘cssURL’)
• Find all divs that do not have an id attribute
• $(‘div’).not(‘[@id]’)
• Find the fifth paragraph on the page, then find
the next element (its direct sibling)
• $(‘p’).eq(4).next()
Contd….
jQuery
• jQuery can take a snippet of HTML and it will
turn into a DOM element
• var div = $(‘<div>Dexter is in lab</div>’);
• div.addClass(‘cssDiv’);
• div.attr(‘id’,’DX’);
• Chaining
• var div = $(‘<div>text</div>’).addClass(‘cd’).attr(‘id’, ‘DX’);
• div.appendTo(‘div#FX’)
Contd….
jQuery
• Find this, do something
$(‘#DX’).show(‘medium’);
$(‘#FX’).addClass(‘css’);
• Chainable events: find this, do several things in
succession
$(‘#GX’).fadeIn(‘slow’).addClass(‘css’).html(‘Text’);
Contd….
How do you assign events?
• Don't add "onclick" to your markup: use jQuery to find
and assign events
$(‘#control’).click(function() {
$(‘#DX’).show().addClass(‘css’);
});
Asynchronous Dynamic Fun
• Asynchronous HTML and HTTP
$(‘#control’).click(function() {
$(‘#DX’).load(‘service.php’);
});
Asynchronous Dynamic Fun
• JSON Style (JavaScript Object Notation)
$(‘#control’).click(function() {
$.getJSON(‘service_json.php’, function(json)() {
alert(json);
});
});
• $.get( url, params, callback )
Load a remote page using an HTTP GET request.
• $.getScript( url, callback )
Loads and executes, a remote JavaScript file using an HTTP GET request.
• $.post( url, params, callback )
Load a remote page using an HTTP POST request.
Contd….
References
• http://www.jquery.com
• http://docs.jquery.com
• http://jquery.bassistance.de/api-browser
• http://www.learningjquery.com
• http://15daysofjquery.com
Q&A
Contact
Abdul Hafeez
habdul@in.com
http://in.linkedin.com/in/habdul
http://twitter.com/hafeez1216
http://drupal.coolpage.biz
http://drupal.org/user/315349

Mais conteúdo relacionado

Mais procurados (20)

Dom structures
Dom structuresDom structures
Dom structures
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Document object model(dom)
Document object model(dom)Document object model(dom)
Document object model(dom)
 
Introduction to the DOM
Introduction to the DOMIntroduction to the DOM
Introduction to the DOM
 
INTRODUCTION TO DOM AND DOM TREE
INTRODUCTION TO DOM AND DOM TREEINTRODUCTION TO DOM AND DOM TREE
INTRODUCTION TO DOM AND DOM TREE
 
Drupal by fire
Drupal by fireDrupal by fire
Drupal by fire
 
Dom structure
Dom structureDom structure
Dom structure
 
Dom structure
Dom structure Dom structure
Dom structure
 
Introductory css and j query
Introductory css and j queryIntroductory css and j query
Introductory css and j query
 
Javascript libraries
Javascript librariesJavascript libraries
Javascript libraries
 
Introduction to j query
Introduction to j queryIntroduction to j query
Introduction to j query
 
Dom structure
Dom structureDom structure
Dom structure
 
fuser interface-development-using-jquery
fuser interface-development-using-jqueryfuser interface-development-using-jquery
fuser interface-development-using-jquery
 
Getting started with jQuery
Getting started with jQueryGetting started with jQuery
Getting started with jQuery
 
Dom
DomDom
Dom
 
A Short Tutorial to Semantic Media Wiki (SMW)
A Short Tutorial to Semantic Media Wiki (SMW) A Short Tutorial to Semantic Media Wiki (SMW)
A Short Tutorial to Semantic Media Wiki (SMW)
 
JavaScript and BOM events
JavaScript and BOM eventsJavaScript and BOM events
JavaScript and BOM events
 
J query
J queryJ query
J query
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Javascipt
JavasciptJavascipt
Javascipt
 

Semelhante a Html dom & j query

FFW Gabrovo PMG - jQuery
FFW Gabrovo PMG - jQueryFFW Gabrovo PMG - jQuery
FFW Gabrovo PMG - jQueryToni Kolev
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptxStefan Oprea
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationLotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationSean Burgess
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxkarthiksmart21
 
JavaScript DOM & event
JavaScript DOM & eventJavaScript DOM & event
JavaScript DOM & eventBorey Lim
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQueryAkshay Mathur
 
Advanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojoAdvanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojoFu Cheng
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)BOSS Webtech
 
Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)Julie Meloni
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handlingsmitha273566
 
SPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentialsSPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentialsMark Rackley
 
SPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQuerySPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQueryMark Rackley
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScriptLilia Sfaxi
 
User Interface Development with jQuery
User Interface Development with jQueryUser Interface Development with jQuery
User Interface Development with jQuerycolinbdclark
 

Semelhante a Html dom & j query (20)

FFW Gabrovo PMG - jQuery
FFW Gabrovo PMG - jQueryFFW Gabrovo PMG - jQuery
FFW Gabrovo PMG - jQuery
 
Web technologies-course 09.pptx
Web technologies-course 09.pptxWeb technologies-course 09.pptx
Web technologies-course 09.pptx
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationLotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
 
JavaScript DOM & event
JavaScript DOM & eventJavaScript DOM & event
JavaScript DOM & event
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
 
Advanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojoAdvanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojo
 
XML Document Object Model (DOM)
XML Document Object Model (DOM)XML Document Object Model (DOM)
XML Document Object Model (DOM)
 
6867389.ppt
6867389.ppt6867389.ppt
6867389.ppt
 
6867389.ppt
6867389.ppt6867389.ppt
6867389.ppt
 
6867389 (1).ppt
6867389 (1).ppt6867389 (1).ppt
6867389 (1).ppt
 
Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)
 
Part 7
Part 7Part 7
Part 7
 
Dom date and objects and event handling
Dom date and objects and event handlingDom date and objects and event handling
Dom date and objects and event handling
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
SPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentialsSPTechCon - Share point and jquery essentials
SPTechCon - Share point and jquery essentials
 
Jquery
JqueryJquery
Jquery
 
SPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQuerySPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQuery
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScript
 
User Interface Development with jQuery
User Interface Development with jQueryUser Interface Development with jQuery
User Interface Development with jQuery
 

Último

Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Último (20)

Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 

Html dom & j query

  • 1. HTML DOM & JQuery “Less is more” Compiled from Wikipedia.org, W3Schools.com & JQuery.com by Abdul Hafeez
  • 2. HTML DOM & JQuery : Agenda • DOM – Document Object Model • DOM History • Node Document Tree • Find & Access Nodes • What is JQuery • Why JQuery • JQuery Philosophy • How to assign events using JQuery • Asynchronous Dynamic Fun
  • 3. DOCUMENT OBJECT MODEL • Platform and language independent standard object model for representing HTML or XML related formats • Level 1 DOM specification is published in 1998 by W3C, and it allowed to and manipulation of every single element. • No cross browser incompatibility • DOM is the way JavaScript sees its containing HTML page • DOM supports navigation in any direction • SAX (Simple API for XML) is a popular alternative to the DOM • DOM is separated in different parts – Core – XML – HTML • And into different levels, Level 1/2/3
  • 4. HTML DOM • Defines a standard way for accessing and manipulating HTML documents • Everything in an HTML document is a node. – Document Node : Entire Document – Element Node : HTML tags – Text Node : Texts in element node – Attribute Node : HTML attribute – Comment Nodes
  • 5. • DOM presents an HTML document as a tree structure (a node tree) with elements, attributes and texts. The tree starts at the document node and continues to branch out until it has reached all text nodes at the lowest level. • All the nodes in a node tree have relationships to each other (e.g. parent/child). HTML DOM Contd….
  • 6. • With JavaScript we can restructure an entire HTML document. We can add, remove, change or reorder items on a page. • To do so, JavaScript needs access to all elements in the HTML document, and this access to the elements is given through DOM. HTML DOM Contd….
  • 7. Node/Document Tree • All nodes have relationships to each other • Except document node, every other node has a parent node • Most element nodes have child nodes • Nodes might share a parent and we call them siblings • Nodes can also have descendants, meaning all the nodes that are children of a node, or children of those children and so on. • Nodes can also have ancestors. Ancestors are nodes that are parents of a node, or parents of this parent and so on.
  • 8. Find & Access Nodes • JavaScript provides methods and properties to find and access nodes • Using getElementById() and getElementByTagName() mothods – It doesn’t follow document structure – Can find any HTML element in the entire document. – Gives us the HTML element(s) we need regardless of where they are in the document – getElementByTagName(‘p’) gives us all the <p> elements in the document – getElementById(‘RT’) returns the correct element whether it is hidden in the tree • Using parentNode, firstChild and lastChild properties of an element node – Follows document structure and allow short-distance travel in the document
  • 9. What is jQuery • An open source JavaScript library that simplifies the interaction between HTML and JavaScript • Is designed to change the way you write JavaScript
  • 10. Why jQuery? • Fully documented • Great community • Tons of plugins • Small size (15kb) • Everything works in IE 6+, Firefox, Safari 2+ and Opera 9+ • Simplifies how you handle events, perform animations or add Ajax interactions • And it is fast • Can learn in 30 minutes
  • 11. jQuery Philosophy • (Almost) every operation boils down to: – Find some stuff – Do something to it
  • 12. jQuery • Select all <div> tags from the document • document.getElementsByTagName(‘div’) • jQuery(‘div’) • $(‘div’) • Select an element whose id is ‘DX’ • document.getElementById(‘DX’) • $(‘#DX’) • $(‘#DX’).hide(‘slow’)
  • 13. jQuery • Add a class (css) to all links in a document • $(‘a’).addClass(‘cssURL’) • Find all divs that do not have an id attribute • $(‘div’).not(‘[@id]’) • Find the fifth paragraph on the page, then find the next element (its direct sibling) • $(‘p’).eq(4).next() Contd….
  • 14. jQuery • jQuery can take a snippet of HTML and it will turn into a DOM element • var div = $(‘<div>Dexter is in lab</div>’); • div.addClass(‘cssDiv’); • div.attr(‘id’,’DX’); • Chaining • var div = $(‘<div>text</div>’).addClass(‘cd’).attr(‘id’, ‘DX’); • div.appendTo(‘div#FX’) Contd….
  • 15. jQuery • Find this, do something $(‘#DX’).show(‘medium’); $(‘#FX’).addClass(‘css’); • Chainable events: find this, do several things in succession $(‘#GX’).fadeIn(‘slow’).addClass(‘css’).html(‘Text’); Contd….
  • 16. How do you assign events? • Don't add "onclick" to your markup: use jQuery to find and assign events $(‘#control’).click(function() { $(‘#DX’).show().addClass(‘css’); });
  • 17. Asynchronous Dynamic Fun • Asynchronous HTML and HTTP $(‘#control’).click(function() { $(‘#DX’).load(‘service.php’); });
  • 18. Asynchronous Dynamic Fun • JSON Style (JavaScript Object Notation) $(‘#control’).click(function() { $.getJSON(‘service_json.php’, function(json)() { alert(json); }); }); • $.get( url, params, callback ) Load a remote page using an HTTP GET request. • $.getScript( url, callback ) Loads and executes, a remote JavaScript file using an HTTP GET request. • $.post( url, params, callback ) Load a remote page using an HTTP POST request. Contd….
  • 19. References • http://www.jquery.com • http://docs.jquery.com • http://jquery.bassistance.de/api-browser • http://www.learningjquery.com • http://15daysofjquery.com
  • 20. Q&A