views:

103

answers:

2

In Javascript, I have a variable that is set to a block of text from a WYSIWYG editor.

I want to use JQUERY's EACH() to find span's with the class XXXX.

$( "span.foods" ).each( function() {});

But I want it to search in the variable=lookhere I have since I don't have access the the text in the WYSIWYG editor directly (CKEDITOR).

How can this be done?

+1  A: 

You want $.each(), which can go over anything, not just a jQuery object. Documentation: http://api.jquery.com/jQuery.each/

Okay, now I understand. What you seem to want will require becoming pretty cozy with javascript string methods and properties, especially split(), and manipulating arrays.

D_N
That will find SPANS with class="foods" ? I'm not seeing how?
AnApprentice
I don't think so, I think he wants to run the CSS selector engine against a string in a variable (if I'm reading the OP correctly), and THEN be able to to iterate the results.
Plynx
@Plynx correct!
AnApprentice
+4  A: 
var html = "...";

Assuming text contains the entire html from your wysiwyg editor, these will work. Basically this parses the HTML, constructs the DOM so we can run selectors on it.

var nodes = $(html);
$("span.foods", nodes).each(..);

Or equivalently

$("span.foods", $(html));
Anurag
AnApprentice
+1, this is better than my suggestion of inserting it into the page.
TM
That's excellent.
D_N
Won't this leak DOM elements? They don't go away just because the collection pointing to them does.
hobbs
This is just a snippet. If this were in a global scope, then yes - `var nodes = $(html);` will keep the object around until the page unloads or we do something about it. From within a function, as long as no circular references are being created, the object will be reclaimed whenever the garbage collector runs after `each()` is done with it.
Anurag
Nevermind, I thought that the `document` held references to `createElement`ed nodes. I was wrong.
hobbs