views:

243

answers:

3

Goal, is to extract the content for the CKEDITOR Text Editor, and then only obtain the FIRST paragraph. For some reason the bellow isn't working... Ideas?

Given the following JavaScript:

var newTitle = CKEDITOR.instances.meeting_notes.getData(); 
newTitle = $(newTitle).find("p:first").text();
+3  A: 

It doesn't work because find() searches the descendants and your paragraph must be at the top level of the HTML you're searching.

For example:

alert($("<p id='one'>one</p><p id='two'>two</p>").find("p:first").attr("id"));

returns "undefined" whereas:

alert($("<p id='one'>one</p><p id='two'>two</p>").filter("p:first").attr("id"));

will output "one".

So you could use filter() if you know it's at the top level (possibly falling back to find()). Alternatively you could wrap the whole lot up in a dummy element:

alert($("<div>" + html + "</div>").find("p:first").text());

Edit: My advice? Use:

newtitle = $(newtitle).filter("p:first").text();
cletus
Interesting, any suggestions on how I can extract the first paragraph then?
AnApprentice
This is interesting, but the FILTER("P") is returning all the paragraphs and what I'm trying to do is just get the first paragraph. Does that make sense?
AnApprentice
I'm lost :) can you show me the line of code your suggesting?
AnApprentice
That did it thank you thank you!!!
AnApprentice
A: 

I am not sure if works for you, but just try putting a space before :first, for some reasons i can't explain this works as far my experience is concerned:

The new selector for find would now be, find("p: first")

var newTitle = CKEDITOR.instances.meeting_notes.getData(); 
newTitle = $(newTitle).find("p :first").text();

BTW can you post some sample values of the newTitle, just curious of what it looks like!

jerjer
That doesn't work since newTitle is an object and not a div. hmmm
AnApprentice
can give us the structure of the object? is it a DOM object or just a JSON object.
jerjer
From the CKEDITOR API: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html{String} getData()Since: 3.0Gets the editor data. The data will be in raw format. It is the same data that is posted by the editor.Defined in: core/editor.js.
AnApprentice
if so, newTitle = $(newTitle).filter("p :first").text();
jerjer
if not, newTitle = $(newTitle).filter("p")[0].text();
jerjer
or newTitle = $$( (newTitle).filter("p").get(0) ).text();
jerjer
A: 

This is completely untested, but assuming that what getData returns is a string of HTML, try this:

newTitle=$("<div>").html(newTitle).find("p:first").text();
icktoofay