Skip to content Skip to sidebar Skip to footer

Jquery Html() Issue

I am trying to put the html markup into my array with the following codes //my htmlData which is entered by user so it could be varies. test here for the testi

Solution 1:

Should be the issue with the text nodes. As they do not have the innerHTML property Try filtering them out

var data = [];
$(htmlData).contents().each(function(){
     var nodeType = this.nodeType;
     if(nodeType === 1) { // Will only select element nodes
        data.push($.trim($(this).html()));
     } 
     else if(nodeType === 3) {
        data.push($.trim(this.nodeValue));
     }
}

You can use .nodeValue to access the data of th etext node


Solution 2:

Assuming that $(htmlData).contents() returns a valid jQuery object, use $.trim($(this).html()) instead of .trim()


Solution 3:

Text nodes don't have innerHTML properties.


Post a Comment for "Jquery Html() Issue"