Hope you can help.
I’m looping through two selectors:
$('.div').each(function() {
$('.selector1, .selector2 option:selected').text();
}
Both selectors returns plain-text and I need the output in the order of the page.
My problem is that whenever it is .selector2 (value from a dropdown) I need to add a < span > tag around it.
How can I do that and at the same time keep the order?
text()returns plain text with no markup. You shouldn’t treat this as HTML, as you imply by ‘adding<span>’. Otherwise,<and&characters in the text will become markup, with potential security implications. Never mix up plain-text and HTML markup.If you must throw HTML strings about, you could use
html()instead:But I’d prefer to use DOM-like methods: