- Prototype - Home
- Prototype - Short Overview
- Prototype - Useful Features
- Prototype - Utility Methods
- Prototype - Element Object
- Prototype - Number Processing
- Prototype - Strings Processing
- Prototype - Array Processing
- Prototype - Hash processing
- Prototype - Basic Object
- Prototype - Templating
- Prototype - Enumerating
- Prototype - Event Handling
- Prototype - Form Management
- Prototype - JSON Support
- Prototype - AJAX Support
- Prototype - Expressing Ranges
- Prototype - Periodical Execution
Prototype - cleanWhitespace() Method
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