A site dedicated to all the cool new stuff in the HTML5 spec.

HTML5 Powered with Connectivity / Realtime, CSS3 / Styling, Device Access, Graphics, 3D & Effects, Multimedia, Performance & Integration, Semantics, and Offline & Storage


Demo

Jan 31, 2011
@ 4:00 pm
sydlawrence
Permalink

3 notes

New Selectors Available

This isn’t exactly HTML5, but I thought I would share it with you anyway. Until recently, to be able to get elements from the DOM which did not have an ID was tricky. So people use the sizzle javascript library or jQuery (which has the sizzle library)

var $el = $('#navbar > ul > li:first-child a');

Now however there are a few extra functions available to you using just standard javascript.

If you want all the elements of a certain class, you can use:

var elements = document.getElementsByClassName('className');

Or to get multiple classes:

var elements = document.getElementsByClassName('className otherClass');

This will fetch all the elements OF either classThere are also a few other functions available to you:

var first = document.querySelector('navbar > ul > li:first-child a');
var ajax_as = document.querySelectorAll('a.ajax');

These functions use all the new css selectors available with css3 :)

To view a demo of these in action click here

To view some sample code click here

The official specification

http://www.w3.org/TR/selectors-api2/

  1. js-html5 posted this