1

On my site, as a little footer I guess, I use the tag <joexn id="yo"> and the javascript

var div = document.getElementById('yo');
div.insertAdjacentHTML( 'beforeBegin', 'joexn.com' );

This produces a watermark/footer and want I want to know is how can I change it so the tag is just <joexn> using Javascript only. I want minimalist code only and no clutter. I tried using document.getElementByTagName('joexn'); but that didn't work.

What's the easiest method?

I don't know why this has been marked as duplicate, it is nothing like that other question.

joexn
  • 25
  • 7
  • div.insertAdjacentHTML( 'beforeBegin', 'joexn' ) ? – numX Jul 02 '16 at 12:04
  • No, that just adds my website link, that won't change. – joexn Jul 02 '16 at 12:05
  • Use `querySelector` or `querySelectorAll` to get a reference. Additionally, you need a closing tag too (``). – Teemu Jul 02 '16 at 12:12
  • `getElementsByTagName` returns a HTMLCollection, just like `getElementsByClassName` explained in the other question. – Oriol Jul 02 '16 at 12:37
  • @joexn Interesting reading: [Custom Elements](http://www.html5rocks.com/en/tutorials/webcomponents/customelements/V). – Teemu Jul 02 '16 at 13:06

2 Answers2

1

try using document.querySelector(): DEMO

var div = document.querySelector('joexn');
div.insertAdjacentHTML( 'beforeBegin', 'joexn.com' );
Amin Jafari
  • 7,157
  • 2
  • 18
  • 43
0

document.getElementsByTagName('joexn') should work. Mind you, you have a typo in the usage of this method. It is getElements and not getElement.

This method returns an HTMLCollection object which is an array-like object. So, to get the element itself, you can do

var joexnElement = document.getElementsByTagName('joexn')[0];

And, then do the rest...

Prashant
  • 7,340
  • 2
  • 25
  • 24
  • That's strange, `getElement` works still. I'll try out this one more time and I'll see if `document.getElementsByTagName('joexn')` works. – joexn Jul 02 '16 at 12:22
  • It seems like `document.querySelector` works perfectly. Thank your for your reply but `document.getElementsByTagName('joexn')` doesn't work for me. – joexn Jul 02 '16 at 12:25