Prototype - cleanWhitespace() Method


Advertisements


This method removes all of element's text nodes, which contain only whitespace and returns element.

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.

Syntax

element.cleanWhitespace();

Return Value

An HTML element

Example

Consider the following example −

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showElements() {
            var element = $('apples');
            alert(element.firstChild.innerHTML);
         }
      </script>
   </head>
   
   <body>
      <ul id = "apples">
         <li>Mutsu</li>
         <li>McIntosh</li>
         <li>Ida Red</li>
      </ul>
      <br />
      
      <input type = "button" value = "showElements" onclick = "showElements();"/>
   </body>
</html>

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>.

Output

Now, let's use cleanWhitespace function and see the result −

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showElements() {
            var element = $('apples');
            element.cleanWhitespace();
            alert(element.firstChild.innerHTML);
         }
      </script>
   </head>

   <body>
      <ul id = "apples">
         <li>Mutsu</li>
         <li>McIntosh</li>
         <li>Ida Red</li>
      </ul>
      <br />
      
      <input type = "button" value = "showElements" onclick = "showElements();"/>
   </body>
</html>

Output


prototype_element_object.htm

Advertisements