0

When i use style property on JavaScript for an existence element it return to me an empty string

alert(element.style.width);

JavaScript return an empty alert box i tried this with many predeclared css property but the same output

var images =    document.querySelectorAll(".slider_image");

document.getElementById("next").onclick = function() {
   for (var i = 0; i < images.length; i++) {
   alert(images[i].style.width);
   }
};

And here is the html

  <div id="first-part">
  <div id="slider">
    <img src="img/1.jpg" class="slider_image" id="1"/>
    <img src="img/2.jpg" class="slider_image" id="2"/>
    <img src="img/3.jpg" class="slider_image" id="3"/>
    <img src="img/next.png" id="next-img"/>
  </div>
  <span id="next">Next</span>  <span id="before">Before</span>
</div>

And here is my css :

.slider_image{
  width:100%;
  position:relative;
  right:0px;
}

What might cause this ?

1

1 Answer 1

9

The .style object only includes style properties directly set on the DOM element. Properties determined from CSS are not visible that way.

Sign up to request clarification or add additional context in comments.

3 Comments

In order to find "calculated styles", check out getComputedStyles ( developer.mozilla.org/en-US/docs/Web/API/… )
But how should i do when i want to edit this value
@Abdou you can set styles on the .style object freely. You just can't use it to examine styles inherited via CSS.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.