最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I convert object HTMLButtonElement to html button? - Stack Overflow

programmeradmin3浏览0评论

Hi I get object HTMLButtonElement due to var btn = document.createElement('button'); this is normal behaviour but how can I get normal button as an graphic instead of object? i.e <button type="button">

like a converting object into a string but in this case object to what?

Hi I get object HTMLButtonElement due to var btn = document.createElement('button'); this is normal behaviour but how can I get normal button as an graphic instead of object? i.e <button type="button">

like a converting object into a string but in this case object to what?

Share Improve this question edited Apr 12, 2016 at 3:41 grzesiekmq asked Apr 12, 2016 at 3:26 grzesiekmqgrzesiekmq 4391 gold badge6 silver badges17 bronze badges 7
  • 2 What's wrong with the object? Do you need to convert it to HTML or something? Usually you can just use the object though. – Alexander O'Mara Commented Apr 12, 2016 at 3:28
  • I want to display a button what props can I use? – grzesiekmq Commented Apr 12, 2016 at 3:29
  • do you want to get the html markup for the button, in that case btn.outerHTML – Arun P Johny Commented Apr 12, 2016 at 3:29
  • If you want to display a button then append the btn instance to an element – Arun P Johny Commented Apr 12, 2016 at 3:29
  • 1 just like jsfiddle/arunpjohny/xq1n18sy/1 – Arun P Johny Commented Apr 12, 2016 at 3:30
 |  Show 2 more ments

2 Answers 2

Reset to default 5

In the dom, Button is stored as instances of HTMLButtonElement object, that is why when you try to convert it to string(calling toString()) you are getting [object HTMLButtonElement].

Since you want to add the button to the view(dom tree), you can just append the button instance to the tree using appendChild() like

var btn = document.createElement('button');
btn.innerText = 'this button';
document.getElementById('container').appendChild(btn);
document.getElementById('markup').innerText = btn.outerHTML;
<div id="container"></div>
<pre id="markup"></pre>

Instead of element use element.HTML ....such properties like .HTML , .value , .Text will not return object htmlbuttonelement .

发布评论

评论列表(0)

  1. 暂无评论