3

I had created an anchor element using createElement() (in both chrome and firefox developer tools) and when I try to display the element in the console, I find blank. When trying the same with div element creation, I can see the element displayed in console correctly. Below is the actual code and output. What is the reason for this? Should not the anchor element output as a: [object HTMLAnchorElement]?

Code: In the console of chrome developer tool

var anchor = document.createElement(‘a’)
console.log(‘anchor: ‘ + anchor)

Output a:

Code:

var div = document.createElement('div')
console.log(‘div: ‘ + div)

Output div: [object HTMLDivElement]

3
  • what is with those quotes? Commented Oct 7, 2016 at 21:35
  • They are string literals. Commented Oct 7, 2016 at 23:34
  • Well the error you would get using them in chrome would be "Uncaught SyntaxError: Invalid or unexpected token" Commented Oct 8, 2016 at 17:35

1 Answer 1

1

The anchor is there, you're just using the console incorrectly.

You're concatenating a string with an object, and anchor.toString() returns an empty string, while with a DIV it would return [object HTMLDivElement].

To solve it, just use the console correctly, either log them seperately

var anchor = document.createElement('a')
console.log('anchor: ')
console.log(anchor)

or use a comma as a seperator

var anchor = document.createElement('a')
console.log('anchor:', anchor)

and you'll log the object as an object, not as a string

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks adeneo for you answer. I also want to understand why the anchor.toString() does not return [object HTMLAnchorElement]. Is there any reference that can clarify this. I was not able to find any.
@raj - I'm not sure why, you'd think that's exactly what it should return when stringified, but it doesn't, and I couldn't find any references as to why it doesn't?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.