Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

anchor element creation javascript

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]

like image 212
raj Avatar asked Jul 19 '26 02:07

raj


1 Answers

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

like image 160
adeneo Avatar answered Jul 20 '26 15:07

adeneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!