Mou
Mou

Reputation: 16282

$(this).html() is not working when jquery version is v1.10.2

the below code is a sample code where $(this).html() is not working when using jquery version v1.10.2 but the same code is working when jquery version is v1.6.4. see my code and give me suggestion how to get rid of this because our product is using jquery version v1.10.2.

$elements.each(function () {
    //alert($(this).text());
    if ($(this).html() === null) {

        $item = $('<li class="current" />').append($(this));
    }
    else {
        if (!isNaN(parseInt($(this).text()))) {
            $item = $('<li />').append($(this));

        }
        else {
            $item = $('<li class="navpage"/>').append($(this));
            //alert($(this).text());
        }
    }
    $list.append($item);
});

full code found in this js fiddle link https://jsfiddle.net/tridip/41s1pq3a/38/

just see & run the jsfiddle code with 2 different version called v1.6.4 and v1.10.2 then understand $(this).html() is not working.

looking for suggestion what i need to change in code as a result $(this).html() will start working in v1.10.2. thanks

Upvotes: 1

Views: 122

Answers (2)

dfsq
dfsq

Reputation: 193261

This comparison

if ($(this).html() === null) {
    $item = $('<li class="current" />').append($(this));
}

is too risky. And apparently it breaks in more recent version of jQuery. Just use

if (!$(this).html()) {
    $item = $('<li class="current" />').append($(this));
}

Demo: https://jsfiddle.net/41s1pq3a/40/

Upvotes: 1

Armfoot
Armfoot

Reputation: 4921

You are invoking the html function directly on TextNodes, a console.log from a $(this) inside the $elements.each returns:

<TextNode textContent="\n           1 \n            ">

This answer explains TextNodes well, and the reasons to operate on them. to Thus you should use the text function instead to get the string you want.

Upvotes: 0

Related Questions