Dom Docs

The Dom library currently provides functions for searching by and manipulating the HTML Element class attribute.

API

isSupported()

Determines if the browser supports the functions in Fork's DOM library. Returns true of so and false otherwise.

Examples

if (FORK.Dom.isSupported()) {
  FORK.Dom.hasClass('foo', 'bar');
} else {
  alert('Your browser does not support the Fork DOM library.');
}

hasClass(element, class)

Test an element to determine if it has a particular class name in it's class attribute. Returns true if the class name is detected or false otherwise.

element
HTMLElement or string id of the element to be tested.
class
string. The class name you wish to detect in the element's class attribute. This string is used in the middle of a regular expression so you can pass in a partial regular expression for this argument. You probably should read the code if you will be using this advanced technique.

Examples

alert(FORK.Dom.hasClass('myDiv', "apple"));
var div = document.getElementById('myDiv');

alert(FORK.Dom.hasClass(div, "apple"));

addClass(element, class)

Adds a class name to the element class attribute if the class name is not already included.

element
HTMLElement or string id of an element.
class
string. The class name you wish to add to the element's class attribute.

Examples

FORK.Dom.addClass('myDiv', "orange");
var div = document.getElementById('myDiv');

var str = "pear";

FORK.Dom.addClass(div, str);

removeClass(element, class)

Removes a class name from the element class attribute if the class name is included.

element
HTMLElement or string id of an element.
class
string. The class name you wish to remove to the element's class attribute.

Examples

FORK.Dom.removeClass('myDiv', "orange");
var div = document.getElementById('myDiv');

var str = "pear";

FORK.Dom.removeClass(div, str);

getElementsByClass(class, options)

Find elements with a particular class name in the class attribute. Returns a JavaScript array of HTML elements. If no elements are found the returned array is empty.

class
string. The class name for which to search.
options
object. An optional argument. There are two independent options. The root option is an HTML Element or string id of an element to be used as the parent node of the search down the DOM tree. The tag option is a string specifying the tag name that all returned elements must have.

Examples

var finds = FORK.Dom.getElementsByClass('orange');
var finds = FORK.Dom.getElementsByClass('orange', {root:'myDiv'});
var finds = FORK.Dom.getElementsByClass('orange', {tag: 'li'});
var div = document.getElementById('div');

var str = 'li';

var finds = FORK.Dom.getElementsByClass('orange', {root:div, tag:str});

Credits

Class name functions modified from Yahoo! UI Dom collection.