Top Banner
Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ
30
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

Bringing pages to lifewith jQuery

BEAR BIBEAULTYEHUDA KATZ

Page 2: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

Bringing pages to lifewith jQuery

• Getting and setting element attributes• Storing custom data on elements• Manipulating element class names• Setting the contents of DOM elements• Storing and retrieving custom data on elements• Getting and setting form element values• Modifying the DOM tree by adding, moving, orreplacing elements

Page 3: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

Bringing pages to lifewith jQuery

• On an almost daily basis, many of us come across web pages that do something that makes us say, “Hey! I didn’t know you could do that!” And being the commensurate professionals that we are (not to mention being insatiably curious about such things), we immediately start looking at the source code to find out how they did it.

Page 4: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1 Working with element properties and attributes

• Properties are intrinsic to JavaScript objects, and each has a name and a value. The dynamic nature of JavaScript allows us to create properties on JavaScript objects under script control.

• Attributes aren’t a native JavaScript concept, but one that only applies to DOM elements. Attributes represent the values that are specified on the markup of DOM elements.

Page 5: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1 Working with element properties and attributes

• Consider the following HTML markup for an image element:

• <img id="myImage" src="image.gif" alt="An image" class="someClass" title="This is an image"/>

Page 6: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1 Working with element properties and attributes

Page 7: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.1 Manipulating element properties

• jQuery doesn’t possess a specific method to obtain or modify the properties of elements.

• Rather, we use the native JavaScript notation to access the properties and their values. The trick is in getting to the element references in the first place.

Page 8: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.1 Manipulating element properties

• Using array indexing on the wrapped set, as in $(whatever)[n]

• Using the get() method, which returns either an individual element by index, or toArray(), which returns an array of the entire set of elements

• Using the each() or map() methods, where the individual elements are made available in the callback functions

Page 9: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.1 Manipulating element properties

• $('*').each(function(n){• this.id = this.tagName + n;• });

Page 10: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.2 Fetching attribute values

• Method syntax: attr(selector)• method can be used either as a read or as a

write operation.

• <img id="myImage" src="image.gif" alt="An image" class="someClass" title="This is an image" data-custom ="some value"/>

Page 11: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.2 Fetching attribute values

• $("#myImage").attr("data-custom")

Page 12: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.3 Setting attribute values

• Method syntax: attr• attr(name,value)• Sets the named attribute to the passed value

for all elements in the wrapped set.

Page 13: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.3 Setting attribute values

• $('*').attr('title',function(index,previousValue) {

return previousValue + ' I am element ' + index + ' and my name is ' + (this.id || 'unset'); });

Page 14: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.3 Setting attribute values

• Method syntax: attr• attr(attributes)

• $('input').attr({ value: '', title: 'Please enter a value' });

Page 15: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.4 Removing attributes

• removeAttr(name)• Removes the specified attribute from every

matched element.

Page 16: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.4 Removing attributes

• The removing an attribute doesn’t remove any corresponding property from the JavaScript DOM element, though it may cause its value to change. For example, removing a readonly attribute from an element would cause the value of the element’s readOnly property to flip from true to false, but the property itself isn’t removed from the element.

Page 17: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.5 Fun with attributes

• EXAMPLE #1—FORCING LINKS TO OPEN IN A NEW WINDOW

• <a href="http://external.com" target="_blank">Some External Site</a>

Page 18: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.1.5 Fun with attributes

• EXAMPLE #2—SOLVING THE DREADED DOUBLE-SUBMIT PROBLEM

• $("form").submit(function() { $(":submit",this).attr("disabled", "disabled");

});

Page 19: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.2 Changing element styling

• If we want to change the styling of an element, we have two options. We can add or remove a class, causing any existing style sheets to restyle the element based on its new classes. Or we can operate on the DOM element itself, applying styles directly.

Page 20: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.2.1 Adding and removing class names

• addClass(names)• Adds the specified class name or class names to

all elements in the wrapped set.• removeClass(names)• Removes the specified class name or class names

from each element in the wrapped set.• toggleClass(names)• Adds the specified class name if it doesn’t exist on

an element, or removes the name from elements that already possess the class name.

Page 21: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.2.1 Adding and removing class names

• function swapThem() {$('tr').toggleClass('striped');}

• $(function(){$("table tr:nth-child(even)").addClass("striped");$

("table").mouseover(swapThem).mouseout(swapThem);});

Page 22: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.2.2 Getting and setting styles

• css(name,value)• Sets the named CSS style property to the

specified value for each matched element.

• $("div.expandable").css("width",function(index, currentWidth) {return currentWidth + 20;});

Page 23: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.2.2 Getting and setting styles

• width(value)• height(value)• Sets the width or height of all elements in the

matched set.

• $("div.myElements").width(500)• is identical to• $("div.myElements").css("width",500)

Page 24: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.2.2 Getting and setting styles

• function displayDimensions() {$('#display').html($('#testSubject').width()+'x'+$

('#testSubject').height());

Page 25: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.2.2 Getting and setting styles

• offset()• Returns the position (in pixels) of the first

element in the wrapped set relative to the document origin.

• position()• Returns the position (in pixels) of the first

element in the wrapped set relative to the element’s closest offset parent.

Page 26: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.3 Setting element content

• Although use of the DOM API methods is certainly exact, it’s also fairly “wordy” and results in a lot of code, much of which can be difficult to visually inspect. In most cases, modifying an element’s HTML is easier and more effective, so jQuery gives us a number of methods to do so.

Page 27: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

3.3.1 Replacing HTML or text content

• html()• Obtains the HTML content of the first element

in the matched set.

• text()• Concatenates all text content of the wrapped

elements and returns it as the result of the method.

Page 28: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.
Page 29: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.
Page 30: Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.