Skip to content Skip to sidebar Skip to footer

Using Getelementbyclassname In Safari

I'm trying to target Safari(both mobile and desktop) and append some styles for a class via Javascript. This is how my code looks like. (function safaristyles() { if (navigato

Solution 1:

document.getElementsByClassName("avatar"); will return you elements is a HTMLCollection of found elements.

so if you have just one item this would work

  avt[0].style.border = "0" + "px";
    avt[0].style.padding = "0" + "px";

if you have multiple items use a for loop.

here is a simple fiddle for something similar http://jsfiddle.net/qjreK/1/

Check these out for more details

https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName

http://www.w3schools.com/htmldom/dom_using.asp

Solution 2:

document.getElementsByClassName() returns a set of elements. If you want to change the style of a class, I see two good options:

  1. Append a style tag with the new class style.
  2. Make a loop and change each item of the document.getElementsByClassName()'s list.

I would recomment to use the first method, because it would allow you to add new elements with that class without needing to change it's style.

Solution 3:

The method getElementsByClassName just return the nodeList match that classname which you pass in, but not array.

You can convert this nodeList to array and set the single item's style

var avt = Array.prototype.slice.call(avt, 0);

avt[0].style.padding = "0px";

Post a Comment for "Using Getelementbyclassname In Safari"