Select A Tag With A Selector From A Text Variable Using Jquery
I have a string which contains text and some tags in it; I want to know how I can select a tag from the variable and loop it. I tried the following but it didn't work: va
Solution 1:
You need to wrap the text in a div (or other element) then find()
it:
var text = 'some string here with <a href="#link">http:something.com</a> more string and more links also';
text = $('<div>' + text + '</div>');
text.find('a').each(function() {
var length = 10;
var end = '...';
varstring = $(this).html();
$(this).html(string.substring(0, length) + (string.length > length ? end : ''));
});
var text = text.html();
// Put it into a textarea
$('#myTextarea').val(text);
Post a Comment for "Select A Tag With A Selector From A Text Variable Using Jquery"