Louis Michael
Louis Michael

Reputation: 398

String variable returned object in Javascript

I have a problem with a variable string but return [Object object]

$(document).ready(function () {
    $(".story-area > h1").text(function () {
        return $(this).text(convertString($(this).text()));
    });

    $(".story-area > p").text(function () {
        return $(this).text(convertString($(this).text()));
    });

    $(".story-area > div > p").text(function () {
        return $(this).text(convertString($(this).text()));
    });
});


function convertString(current_text) {
    var arr_text = current_text.split(' ');
    var new_text = '';
    for (i = 0; i < arr_text.length; i++) {
        if (arr_text[i].length > 4) {
            new_text += arr_text[i].replace(/[Hh][Ii]/g, 'HIV') + ' ';
        } else {
            new_text += arr_text[i] + ' ';
        }
    }
    return new_text;
}

The new_text value returns [Object object] instead of the string value. Any errors on my code?

enter image description here

Upvotes: 1

Views: 870

Answers (1)

epascarello
epascarello

Reputation: 207511

AH, you are setting the text inside of setting the text! You are treating text() as an each.

$(".story-area > h1").text(function () {
    return convertString($(this).text());
});

Upvotes: 3

Related Questions