1

i have this code.

<p>jQuery is free, open source software, dual-licensed under the <span>MIT License</span> or the GNU General Public License, Version 2.</p>
<p><span id="elt">jQuery is a</span> cross-browser <span>JavaScript</span>library designed to simplify the client-side scripting of HTML. <span>It was released in January 2006</span> at BarCamp NYC by John Resig. Used by over 52% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use <span>today</span>.</p>
<img src="character1.jpg" height="200"/>
<p>jQuery is free, open source software, dual-licensed under the <span>MIT License</span> or the GNU General Public License, Version 2.</p>
<p>...

i want to get the first 500 characters start with $(".elt") ,like this:

jQuery is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML.It was released in January 2006 at BarCamp NYC by John Resig. Used by over 52% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use today.jQuery is free, open source software, dual-licensed under the MIT License or the GNU General Public License, Version 2.

just text, remove all the html tags.

user615816
  • 439
  • 4
  • 16

1 Answers1

4
$('#elt').parent().text().slice(0, 500);

Gets text, takes first 500 characters

EDIT: sorry, read question not code sample. Fixed.

var node = $('#elt').parent();
var text = node.text();

while (text.length < 500) {
    node = node.nextSibling;
    if (node.nodeType === 1) {
        text += node.text();
    } else if (node.nodeType === 3) {
        text += node.nodeValue;
    }
}

text = text.slice(0, 500);
callumacrae
  • 8,185
  • 8
  • 32
  • 49
  • sorry , my english sucks, there are also text before $('#elt') , and i only want the text start with $('#elt'). – user615816 Mar 21 '12 at 12:13
  • Could you give a few smaller examples with only, say, 20 characters? – callumacrae Mar 21 '12 at 12:14
  • what i mean is , find the element , and get its text , `text1`. and then get all the text after this element , `text2` , then `(text1+text2).slice(0, 500);` – user615816 Mar 21 '12 at 12:40