2

I know using something like document.getElementById("img1").attribute is an standard way to access to elements, but what about img1.attribute? Is it OK to use it?

Few years ago I used to use NameValue.attribute (I mean when I had an element like this <input name="NameValue">) to get and set elements attributes but today I tested it on FF and it didn't work. So I tested IdValue.attribute on IE, FF and Chrome and it worked.

Léo Lam
  • 3,870
  • 4
  • 34
  • 44
Vahid2023
  • 863
  • 3
  • 11
  • 33
  • Note: adding tag names in the post title is unnecessary, since the system does that for you automatically. – Léo Lam Apr 29 '15 at 16:54
  • 2
    i wouldn't trust it - it might just fail in strict mode. – Daniel A. White Apr 29 '15 at 17:00
  • 3
    It would be best if you used `document.getElementById`, you can use the ID/global var method but you never know when that will stop working and it might not work on all browser/devices – Huangism Apr 29 '15 at 17:00
  • While you can do this, please don't. It's unconventional and might cause obscure bugs when you expand your code later. – Jonathan Apr 29 '15 at 17:06
  • Actually I'm creating a video tutorial on Web Design & Programming and I haven't teached "functions" yet, so I wanted to use IdValue.attribute to make learning easier for the users , But it seems I have to teach "functions" first, Thank you all very much , – Vahid2023 Apr 29 '15 at 19:01

0 Answers0