Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jephjohnson/2871197 to your computer and use it in GitHub Desktop.
Save jephjohnson/2871197 to your computer and use it in GitHub Desktop.
Get the words from a string in multiple divs using a for loop with jQuery or regular expression
$(function() {
$("#d0, #d1, #d2").each(function() {
var text = $(this).text();
var textArr = text.split("*");
$(this).html("<ul></ul>");
for(var i = 0; i < textArr.length; i++) {
$(this).find("ul").append('<li><a>' + textArr[i] + '</a></li>');
};
});
});
(function($) {
$.fn.textTolist = function() {
var textArr = $(this).text().split('*'),
target = $(this).empty().append('<ul></ul>');
for (var i = 0; i < textArr.length; i++) {
$('ul', target).append('<li><a>' + textArr[i] + '</a></li>');
}
};
})(jQuery);
$('#d0, #d1, #d2').each(function() {
$(this).textTolist();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment