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
2 Answers
Reset to default 5In 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 .