howto, Use JQUERY .EACH() but not for a element, for a Variable
-
19-09-2019 - |
Question
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?
Solution
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.
OTHER TIPS
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));