Eric F. Alsheimer
Eric F. Alsheimer

Reputation: 71

What is the Prototype equivalent of JQuery's $(element).text()?

Given the following snippet:

<div id="myDiv">
  This is my text <span>with a span</span>
</div>    

JQuery can get the interior string with:

$('#myDiv').text();

Is there a more intuitive way in Prototype than:

$('myDiv').pluck('innerHTML').first().stripTags();

Upvotes: 7

Views: 2565

Answers (1)

Fabien M&#233;nager
Fabien M&#233;nager

Reputation: 140195

Hum, doesn't

$('myDiv').innerHTML.stripTags();

work ?

Edit: if you really want a text() method in Prototype, you can do so :

Class.extend(Element, {
  text: function(element) {
    return element.innerHTML.stripTags();
  }
};

and then use it like this :

var txt = $('myDiv').text();

Upvotes: 4

Related Questions