18
JavaScript Training Goal Trainers Format Lecture Exercises Ask Questions! bitovi/js-trainin

Finding elements in the DOM

Embed Size (px)

Citation preview

Page 1: Finding elements in the DOM

JavaScript Training

Goal Trainers Format

• Lecture• Exercises• Ask Questions!• bitovi/js-training

Page 2: Finding elements in the DOM

Elements

Page 3: Finding elements in the DOM

The Goal

Page 4: Finding elements in the DOM

The Setup

<ul id="breeds"><li><a href="#doberman">Doberman</a></li><li><a href="#beagles">Beagles</a></li><li><a href="#boxer">Boxer</a></li>

</ul><div id="doberman">

<img src="doberman.jpg" width="400px"/></div><div id="beagles">

<img src="beagle.jpg"/></div><div id="boxer">

<img src="boxer.jpg" width="400px"/></div>

Page 5: Finding elements in the DOM

The Setup (exercises/DOM/my_jquery.js)

$ = function(selector) { /*...*/ };

$.extend($.prototype, {html: function(string) { /*...*/ },val: function(value) { /*...*/ },text: function(string) { /*...*/ },find: function(el) { /*...*/ },next: function() { /*...*/ },prev: function() { /*...*/ },parent: function() { /*...*/ },children: function() { /*...*/ },attr: function(attr, val) { /*...*/ },css: function(style, val) { /*...*/ },width: function() { /*...*/ },hide: function() { /*...*/ },show: function() { /*...*/ }

});

Page 6: Finding elements in the DOM

Finding

Page 7: Finding elements in the DOM

from the Document

document.getElementById( id )document.getElementsByTagName( tag )document.getElementsByClassName( className )document.querySelector( selector )document.querySelectorAll( selector )

We’re provided a simple API to find elementswithin a document.

Page 8: Finding elements in the DOM

from the Document

document.getElementById( id )

document.getElementById('breeds') //-> HTMLUListElement

<ul id="breeds"><li><a href="#doberman">Doberman</a></li><li><a href="#beagles">Beagles</a></li><li><a href="#boxer">Boxer</a></li>

</ul>

Page 9: Finding elements in the DOM

from the Document

document.getElementsByTagName( tag )

document.getElementsByTagName('li')//-> [HTMLIElement, HTMLLIElement, HTMLLIElement]

<ul id="breeds"><li><a href="#doberman">Doberman</a></li><li><a href="#beagles">Beagles</a></li><li><a href="#boxer">Boxer</a></li>

</ul>

Page 10: Finding elements in the DOM

from the Document

document.getElementsByClassName( className )

document.getElementsByClassName('contact')//-> [HTMLDivElement]

<h1 id="greet">Hello World</h1> <div class="contact"> <label>Age</label> <input name="age"/> </div><label>For</label><input name="user"/>

Page 11: Finding elements in the DOM

from the Document

document.querySelectorAll( selector )

document.querySelectorAll('#doberman img')//-> [HTMLImageElement]

<div id="doberman"><img src="doberman.jpg" width="400px"/>

</div>

Page 12: Finding elements in the DOM

Exercise

Create a function named ‘$’ that takes a selector as an argument, selects elements from the DOM, and returns an array-like object:

var breeds = new $('#breeds');

breeds.length //-> 1;breeds[0] //-> $[ ul#breeds ]

hint:make an “array-like” object, set length to 0, add items with:

[].push.apply(this, items)

Page 13: Finding elements in the DOM

Exercise

Add an html method to get/set the innerHTML of an element(s).

var dogs = new $('#breeds li');

dogs.html('<div>All Dogs</div>').html()//-> '<div>All Dogs</div>'

hint:html() should be “chainable”, returning the original $ instance when setting.

Page 14: Finding elements in the DOM

Exercise

Add an val method to get/set the value of an element.

<input type="text" value="some text">

$('input').val() //-> 'some text'$('input').val('new text');

Page 15: Finding elements in the DOM

Exercise

Remove the need to call new when using our jQuery object.

var dogs = $('#breeds li');

dogs.html('<div>Go To Heaven</div>').html()//-> '<div>Go To Heaven</div>'

Page 16: Finding elements in the DOM

Exercise

Add an text method to get/set the text of an element.

$('ul').text() //-> '// Doberman// Beagles// Boxer// '

$('ul li:first-child').text('TEETH!').text() //-> 'TEETH!'

Page 17: Finding elements in the DOM

from an Element

element.getElementById( id )element.getElementsByTagName( tag )element.getElementsByClassName( className )element.querySelector( selector )element.querySelectorAll( selector )

document.getElementById('#breeds')

.querySelectorAll('li a');

Elements have the same query methods as document.

Page 18: Finding elements in the DOM

Exercise

Add a find method that returns items within the current elements:

var dogImages = $('div').find('img');

hint: Have $ also accept an array of nodes.Use el.querySelectorAll('img').