A
A
Alexey Nikolaev2015-09-19 21:31:36
JavaScript
Alexey Nikolaev, 2015-09-19 21:31:36

How to insert html code into textNode.textContent?

Good evening!
I get a text node from some tag, and replace its content by overriding the textContent property . The process of getting a text node is implemented in jQuery, and unfortunately it doesn't have properties like innerHTML . The code is below:

// Получение текстовых нод
function getTextNode(element) {
    return $(element).addBack().contents().filter(function() {
        return this.nodeType == 3;
    });
}

// Переопределение. $target уже присвоен результат getTextNode
$target.first()[0].textContent = 'Линия текста <br /> Линия текста';

How to insert html in the process of redefinition?
I would be grateful for advice, thanks.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Inchin ☢, 2015-09-19
@Heian

No, that's why it's a text node.
In general, you can do something like this:

$.fn.getTextNode = function() {
    return this.contents().filter(function() {
        return this.nodeType == 3;
    });
}

$.fn.setTextNode = function(t){
  this.before(t).remove();
}

//Получим текстовые ноды и установим одной из них значение
$(elem).getTextNode().first().setTextNode("<div></div>");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question