cleanWhitespace :: Element

cleanWhitespace(element) -> HTMLElement

 

Removes all of element's text nodes which contain only whitespace. Returns element.

 

cleanWhitespace() removes whitespace-only text nodes. This can be very useful when using standard methods like nextSibling, previousSibling, firstChild or lastChild to walk the DOM.

 

If you only need to access element nodes (and not text nodes), try using Element's new up(), down(), next() and previous() methods instead. You won't regret it!

 

Example

 

Consider the following HTML snippet:

 

<ul id="apples">

  <li>Mutsu</li>

  <li>McIntosh</li>

  <li>Ida Red</li>

</ul>

 

Let's grab what we think is the first list item:

 

var element = $('apples');

element.firstChild.innerHTML;

// -> undefined

 

That doesn't seem to work to well. Why is that ? ul#apples's first child is actually a text node containing only whitespace that sits between <ul id="apples"> and <li>Mutsu</li>.

 

Let's remove all this useless whitespace:

 

element.cleanWhitespace();

 

That's what our DOM looks like now:

 

<UL id="apples"><LI>Mutsu</LI><LI>McIntosh</LI><LI>Ida Red</LI></UL>

 

And guess what, firstChild now works as expected!

 

element.firstChild.innerHTML;

// -> 'Mutsu'

 

 


Prototype API 1.5.0 - prototypejs.org