Skip to content

From jQuery Manipulation

alanhogan edited this page Jul 29, 2011 · 9 revisions

‹ Part of the “Nokogiri for jQuery Users” series

jQuery Manipulation methods (and their descriptions) along with Nokogiri equivalents.

.addClass()

Adds the specified class(es) to each of the set of matched elements.

Nokogiri (given name = the class name):

node_set.add_class(name)

(See add_class.)

Or on just one node:

my_node['class'] ||= ""
my_node['class'] = my_node['class'] << " " << name

This should be less verbose, but more opaque — you are creating a NodeSet with just the one node, and then using NodeSet#add_class:

Nokogiri::NodeSet.new(my_node.document, [my_node]).add_class(name)

.after()

Insert content, specified by the parameter, after each element in the set of matched elements.

In Nokogiri, it matters whether .after is called on a Node or NodeSet. Calling .after on a NodeSet only inserts content after the last Node in the NodeSet (equivalent to my_node_set.last.after(content)).

On a Node, .after works as you would expect.

On a NodeSet, the jQuery equivalent would be:

my_node_set.each do |node|
  node.after content
end

See also Node#add_next_sibling.

.append()

Insert content, specified by the parameter, to the end of each element in the set of matched elements.

On one Node:

my_node.add_child some_child   # also returns the new child node

On a NodeSet:

node_set.each do |node|
  # create the new child perhaps using Nokogiri::XML::Builder
  node.add_child new_child
end

.appendTo()

Insert every element in the set of matched elements to the end of the target.

To document.

.attr()

Get the value of an attribute for the first element in the set of matched elements.

Nokogiri: attr(key, value=nil, &blk)

Set the attribute key to value or the return value of blk (a block) on all Node objects in the NodeSet.

Also note that set is an alias for attr; and to remove an attribute, you can call remove_attr.

More here.

.before()

Insert content, specified by the parameter, before each element in the set of matched elements.

Nokogiri .before inserts the content only before the first Node in a NodeSet. See .after() on this page, and also Node#add_previous_sibling.

.clone()

Create a deep copy of the set of matched elements.

To document.

.css()

Get the value of a style property for the first element in the set of matched elements.

To document.

.detach()

Remove the set of matched elements from the DOM.

Nokogiri NodeSet#remove (an alias of NodeSet#unlink).

.empty()

Remove all child nodes of the set of matched elements from the DOM.

To document.

.hasClass()

Determine whether any of the matched elements are assigned the given class.

To document.

.height()

Get the current computed height for the first element in the set of matched elements.

To document.

.html()

Get the HTML contents of the first element in the set of matched elements.

To document.

.innerHeight()

Get the current computed height for the first element in the set of matched elements, including padding but not border.

To document.

.innerWidth()

Get the current computed width for the first element in the set of matched elements, including padding but not border.

To document.

.insertAfter()

Insert every element in the set of matched elements after the target.

To document.

.insertBefore()

Insert every element in the set of matched elements before the target.

To document.

.offset()

Get the current coordinates of the first element in the set of matched elements, relative to the document.

To document.

.outerHeight()

Get the current computed height for the first element in the set of matched elements, including padding, border, and optionally margin.

To document.

.outerWidth()

Get the current computed width for the first element in the set of matched elements, including padding and border.

To document.

.position()

Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.

To document.

.prepend()

Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.

On one Node:

my_node.children.first.before content
# or perhaps:
my_node.children.first.add_previous_sibling node

On a NodeSet:

node_set.each do |node|
  node.children.first.before content
  # Or perhaps
  # create the new child perhaps using Nokogiri::XML::Builder
  node.children.first.add_previous_sibling new_child
end

.prependTo()

Insert every element in the set of matched elements to the beginning of the target.

To document.

.prop()

Get the value of a property for the first element in the set of matched elements.

To document.

.remove()

Remove the set of matched elements from the DOM.

Nokogiri NodeSet#remove (an alias of NodeSet#unlink).

.removeAttr()

Remove an attribute from each element in the set of matched elements.

To document.

.removeClass()

Remove a single class, multiple classes, or all classes from each element in the set of matched elements.

To document.

.removeProp()

Remove a property for the set of matched elements.

To document.

.replaceAll()

Replace each target element with the set of matched elements.

To document.

.replaceWith()

Replace each element in the set of matched elements with the provided new content.

To document.

.scrollLeft()

Get the current horizontal position of the scroll bar for the first element in the set of matched elements.

To document.

.scrollTop()

Get the current vertical position of the scroll bar for the first element in the set of matched elements.

To document.

.text()

Get the combined text contents of each element in the set of matched elements, including their descendants.

To document.

.toggleClass()

Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.

To document.

.unwrap()

Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.

To document.

.val()

Get the current value of the first element in the set of matched elements.

To document.

.width()

Get the current computed width for the first element in the set of matched elements.

To document.

.wrap()

Wrap an HTML structure around each element in the set of matched elements.

To document.

.wrapAll()

Wrap an HTML structure around all elements in the set of matched elements.

To document.

.wrapInner()

Wrap an HTML structure around the content of each element in the set of matched elements.

To document.