javascript - Converting DIV element into String - Stack Overflow

I have a div element that I want to be printed on the page when I click a Create Button.Thus, when I cl

I have a div element that I want to be printed on the page when I click a Create Button.

Thus, when I click create I call a function that has: document.getElementById("createdDiv").textContent = document.querySelector("[data-feed]");

This finds my div element and prints to the page [object HTMLDivElement]

However, when I print the element to the console, I get my div element:

<div data-feed class="feed-element" ... ></div>

I know the console has a toString function that converts the div element into a string but I am not sure how to do this in javascript so I can print the same string to the page. Any suggestions?

I have a div element that I want to be printed on the page when I click a Create Button.

Thus, when I click create I call a function that has: document.getElementById("createdDiv").textContent = document.querySelector("[data-feed]");

This finds my div element and prints to the page [object HTMLDivElement]

However, when I print the element to the console, I get my div element:

<div data-feed class="feed-element" ... ></div>

I know the console has a toString function that converts the div element into a string but I am not sure how to do this in javascript so I can print the same string to the page. Any suggestions?

Share Improve this question asked Oct 9, 2014 at 16:45 cchingcching 7991 gold badge8 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You could use outerHTML:

document.getElementById("createdDiv").textContent = document.querySelector("[data-feed]").outerHTML;

document.getElementById("createdDiv").textContent = document.querySelector("[data-feed]").outerHTML;
[data-feed]::before {
  content: 'The source element: ';
  color: #f00;
}

#createdDiv {
  white-space: pre-wrap;
  border: 1px solid #000;
  padding: 0.5em;
  border-radius: 1em;
}
<div data-feed="something"><span>Text in here</span> with <em>various</em> <strong>elements</strong></div>
<div id="createdDiv"></div>

In order to remove HTML from any childNodes, then you could use a function to clone the node, remove the children, and then return only the outerHTML of that specific node:

function tagHTMLOnly(elem) {
  var temp = elem.cloneNode();
  while (temp.firstChild) {
    temp.removeChild(temp.firstChild);
  }
  return temp.outerHTML;
}

document.getElementById("createdDiv").textContent = tagHTMLOnly(document.querySelector("[data-feed]"));

function tagHTMLOnly(elem) {
  var temp = elem.cloneNode();
  while (temp.firstChild) {
    temp.removeChild(temp.firstChild);
  }
  return temp.outerHTML;
}

document.getElementById("createdDiv").textContent = tagHTMLOnly(document.querySelector("[data-feed]"));
[data-feed]::before {
  content: 'The source element: ';
  color: #f00;
}
#createdDiv {
  white-space: pre-wrap;
  border: 1px solid #000;
  padding: 0.5em;
  border-radius: 1em;
}
<div data-feed="something"><span>Text in here</span> with <em>various</em>  <strong>elements</strong>
</div>
<div id="createdDiv"></div>

References:

  • Element.outerHTML.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745222945a4617332.html

相关推荐

  • javascript - Converting DIV element into String - Stack Overflow

    I have a div element that I want to be printed on the page when I click a Create Button.Thus, when I cl

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信