user7304522
user7304522

Reputation:

Replacing this() with a new value in JavaScript

I'm iterating through every element of a class, and I'm replacing all ** with bold tags.

However, it isn't outputting as I want. I used window alerts, and checked, and the regex is converting it correctly, however the $(this).html = txt doesn't seem to be working. I've tried using innerHTML, val. Nothing seems to work.

The code commented out does work, however I can't refer to all of the Id's because it's possible for new ones to be added, hence me iterating through each element of the class.

    $(document).ready(function () {
    setTimeout(function(){
        var bold = /\*\*(\S(.*?\S)?)\*\*/gm;
        $(".grid-text").each(function() {
            var currentElement = $(this).html();
            var txt = currentElement.replace(bold, '<strong>$1</strong>');
            $(this).html= txt;
        });
        // var s = document.getElementById("001_1_1").innerHTML;
        // var txt = s.replace(bold, '<strong>$1</strong>');
        // document.getElementById("001_1_1").innerHTML = txt;
    }, 3000);
});

Upvotes: 0

Views: 79

Answers (2)

Brooks
Brooks

Reputation: 149

Try passing txt as a parameter to the .html() function:

$(this).html(txt);

Upvotes: 1

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14604

.html is a function:

$(this).html(txt);

Upvotes: 2

Related Questions