views:

305

answers:

4

Given a block of text in a TEXT EDITOR (CKEDITOR), with paragraphs contained in PARAGRAPH tags, how can I do in JQUERY to extract only the first paragraph

Example:

blah blah blah blah blah

blah blah blah 123123blah blah

blah blah blah blah b1212lah

blah blah blah blaasdasdadsh blah

To Just:

blah blah blah blah blah

Thanks

+4  A: 

You can use the :first selector:

$('p:first');
Brian McKenna
make sure you include the specific `div` as well, otherwise you'll only get the very first `p` on the page
Jason
That doesn't seem to be working, please see comment above. thxs
AnApprentice
A: 

I don't know if CKEditor would affect this in any way, but with jQuery you should be able to use:

$("p:first").text();

To get the text in the first <p> element. If the paragraphs are wrapped by a <div> or another element with a specific ID, you can do:

$("#id-of-div p:first").text();
vonconrad
Since the content is in CKEDITOR (iframed) I have the content in a variable not a DIV, how can I use a variable in your second example? thxs
AnApprentice
Try the following: `$(newTitle).find("p:first").text();`
vonconrad
That doesn't seem to work... Full line: var newTitle = CKEDITOR.instances.meeting_notes.getData();newTitle = $(newTitle).find("p:first").text();
AnApprentice
What exactly is the output of newTitle? Can you update your original post with the full contents of that variable?
vonconrad
+2  A: 

Since CKeditor is in an iframe, you might need something along the lines of:

$("#cke_contents_editor1 iframe").contents().find('p:first').text()

to extract the text.

Adam Benzan
This doesn't seem to work: newTitle = $(newTitle).contents().find('p:first').text();
AnApprentice
A: 

http://api.jquery.com/first/

absolutnie