Answer the question
In order to leave comments, you need to log in
JavaScript node parser?
In general, I make my text editor on contenteditable, of course there are functions for bold, italic and underlined text on selection, I need to make sure that each element is entered into a separate object, let's say what I enter into the textarea, and what object should I get:
<b>привет, <i>сосед</i></b>, как дела?
{
"text1":{
"text": "привет, ",
"bold": true
},
"text2":{
"text": "сосед",
"bold": true.
"italic": true
},
"text3":{
"text":", как дела?"
}
}
function parseNode(node) {
let result = [];
if (node.hasChildNodes()) {
let children = node.childNodes;
for (var i = 0; i < children.length; ++i) {
let obj = { 'text': children[i].textContent };
if (children[i].nodeName === "B") obj.bold = true;
if (children[i].nodeName === "I") obj.italic = true;
if (children[i].hasChildNodes() && children[i].childNodes[0].nodeName === "B") { obj.bold = true; }
if (children[i].hasChildNodes() && children[i].childNodes[0].nodeName === "I") { obj.italic = true; }
if (children[i].hasChildNodes() && children[i].childNodes[0].nodeName === "U") { obj.undeline = true; }
result.push(obj);
}
}
return result;
}
<b>Привет, <i>сосед</i></b>
[Object
{
bold:true,
text:"Привет, сосед"
}
]
[Object{
text:"Привет,",
bold: true
}, Object{
text:"сосед",
bold: true,
italic: true
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question