Search code examples
jqueryhtmlsubstringsubstr

Return a html tag with substr JQuery?


I want to add this html tag <a href="#">Read More</a> after following jquery

$("span.fine-print").text(function(index, currentText) {
       if (currentText.length > 200) {
         return currentText.substr(0, 200)+'...';

    }
});

How to add that "Read More" link after this. Any help highly appriciate. Thanks,


Solution

  • You'll have to use the html() method to get and insert HTML, the text() method only works with ... text.

    $("span.fine-print").html(function(index, currentHTML) {
        if (currentHTML.length > 200) {
            return currentHTML.substr(0, 200) + '...<a href="#">Read More</a>';
        }
    });
    

    FIDDLE