AnApprentice
AnApprentice

Reputation: 110980

JQUERY FIND:FIRST not finding the first Paragraph?

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();

Upvotes: 1

Views: 1430

Answers (3)

icktoofay
icktoofay

Reputation: 129019

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();

Upvotes: 0

cletus
cletus

Reputation: 625147

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();

Upvotes: 2

jerjer
jerjer

Reputation: 8770

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!

Upvotes: 0

Related Questions