0

I tried to add an element with JScript code, I gave it href and title attributes... but the link is invisible on the page ( if I check through browsers elements, it highlights an empty space which says "a 0x17". What's wrong with it? What should I do, to make it visible?

    const a = document.createElement('a');
    const b = document.querySelector('body');
    
    a.setAttribute("href", "https://en.wikipedia.org/wiki/Kola_Superdeep_Borehole")
    a.setAttribute("title", "Blah-blah");
    b.appendChild(a);

if I write a.textContent("...") it's visible, but it's just a text, not a link. When I gave it href attribute, it became invisible.

  • Is it JavaScript or JScript? Your title says one but the body and tags say the other. – John Montgomery Dec 09 '20 at 00:30
  • Does this answer your question? [Set Text of Anchor Tag In Javascript](https://stackoverflow.com/questions/3496280/set-text-of-anchor-tag-in-javascript) – John Montgomery Dec 09 '20 at 00:33
  • Does this answer your question? [How do I create a link using javascript?](https://stackoverflow.com/questions/4772774/how-do-i-create-a-link-using-javascript) – JBallin Dec 09 '20 at 00:34

1 Answers1

1

0x17 is the size of the element in the window. What happened here is your link element does not have content (between <a></a> tags. It needs any content to be displayed :

const a = document.createElement('a');
const b = document.querySelector('body');
a.innerHTML = 'Link content';
a.setAttribute("href", "https://en.wikipedia.org/wiki/Kola_Superdeep_Borehole")
a.setAttribute("title", "Blah-blah");
b.appendChild(a);
F.Igor
  • 4,119
  • 1
  • 18
  • 26