It's the same thing that happens if you do the following:
var prueba = {a: 1, b: 2};
console.log(String(prueba));
// [object Object]
If you wanted a representation of that object in the form of String
you should use JSON.stringify
:
var prueba = {a: 1, b: 2};
console.log(JSON.stringify(prueba));
// {"a":1,"b":2}
The same goes for other types of objects. When using the getElementsByClassName
method returns an HTMLCollection
object (it contains all the elements in your document that have that class), so if you try to convert it to String
you will get [object HTMLCollection]
. To return a String
representation of an HTML
element, you should use the innerHTML
methods (for a% representation% of the content of the element) or String
for a outerHTML
representation of the element itself. You should only bear in mind that these methods should be used in an object of String
, so if you want to do it in an object of type Element
you must iterate in it:
var elementos = document.getElementsByClassName("prueba");
[].forEach.call(elementos, function (el) {
console.log(el.outerHTML);
});
<div class="prueba">uno</div>
<div class="prueba">dos</div>
<div class="prueba">tres</div>
Although if your code should be supported at least by HTMLCollection
, to select a single element per class, instead of using Internet Explorer 8
you could use getElementsByClassName
, which directly returns an object of type querySelector
instead of an object type Element
since it selects the first element that matches the selector:
var elemento = document.querySelector(".prueba");
console.log( elemento.outerHTML );
<div class="no-prueba">uno</div>
<div class="prueba">dos</div>
<div class="no-prueba">tres</div>