0

I know similar questions have been asked, like this one, but I've not been able to find a solution that works.

Can someone suggest how I could fetch the value of the width attribute from the CSS class so I can write it to the div?

I want to fetch the actual value set in the class, even if the actual element has been stretched further due to content.

Happy to do this with jQuery if possible.

document.getElementById("myDiv").innerHTML = 'value of CSS width';
#myDiv {
    width: 177px; 
    border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="myDiv">Hello World!</div>
Community
  • 1
  • 1
Daniel Williams
  • 2,115
  • 6
  • 27
  • 46
  • Are you looking to get the css value that's in effect on the div, so do you care if there's multiple styles that apply but are getting overridden? – Jeff Puckett Apr 27 '16 at 01:16
  • I want to fetch the actual value set in the class, even if the actual element has been stretched further due to content. – Daniel Williams Apr 27 '16 at 22:23
  • [this](http://stackoverflow.com/questions/395341/get-element-stylesheet-style-in-javascript) doesn't answer your question, but some of the approaches might help you get the style specified instead of the computed style – Jeff Puckett Apr 27 '16 at 22:31
  • Thanks. I think I'm just going to hardcode in a number instead of trying to fetch it from the class. I'm not a big fan of these type of hacks. – Daniel Williams Apr 27 '16 at 22:33

2 Answers2

0

This article from the jQuery documentation may help you. http://api.jquery.com/css/

Try:

$('#myDiv').css('width')
NancyH
  • 21
  • 1
  • 4
0

That's easy. Try

$('#myDiv').width()
Alexander Elgin
  • 6,326
  • 4
  • 35
  • 48