0

function displayWidth(){
  
  document.getElementById("navbar").className = "navbar-after-click";
  alert(document.getElementById("navbar").style.width);
  
  }
#navbar {
  background-color:red;
  height:200px;
  }
.navbar{
   width :220px;
}
.navbar-after-click{
    width:60px;
}
<html>
  <body>
    <div id="navbar" class="navbar">
      
    </div>
    <button type="button" onclick="displayWidth();">show width </button>
  </body>
</html>

I'm not too familiar with Javascript, that is why I am asking this question. In the above code I tried to alert a CSS property value using Javascript. But it doesn't alert any value as I expected. Is there any wrong with my code? How can I fix this?

10
  • 1
    You should use .offsetWidth property : alert(document.getElementById("navbar").offsetWidth); Commented Oct 21, 2016 at 4:07
  • 1
    you can use offsetWidth to get the width of your div element Commented Oct 21, 2016 at 4:07
  • 1
    You can use getComputedStyle() as explained in this question and various other duplicates. If you use the .style object you only get inline styles. Commented Oct 21, 2016 at 4:07
  • 1
    Possible duplicate of How to get an HTML element's style values in javascript? Commented Oct 21, 2016 at 4:09
  • 1
    @WhiteMaskers Use getComputedStyle to get the width of navbar Commented Oct 21, 2016 at 4:15

5 Answers 5

3

Use getComputedStyle

Plknr Demo:

http://plnkr.co/edit/BQEdwqeZgZ1Nc02ZeAzQ?p=preview

Stack Snippet:

function displayWidth(){
  
  document.getElementById("navbar").className = "navbar-after-click";
  var nav = document.getElementById("navbar");
   var navWidth = window.getComputedStyle(nav,null).getPropertyValue("width");
  alert(navWidth);
  
  }
#navbar {
  background-color:red;
  height:200px;
  }
.navbar{
   width :220px;
}
.navbar-after-click{
    width:60px;
}
<html>
  <body>
    <div id="navbar" class="navbar">
      
    </div>
    <button type="button" onclick="displayWidth();">show width </button>
  </body>
</html>

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

1 Comment

Glad that it helped. Have a great day
1

use this script for you code

      <script type="text/javascript">
            function displayWidth(){
      var element = document.getElementById('navbar');
       var style = window.getComputedStyle(element);
 alert(style.width);  //style. all possible objects list in the end

      }
        </script>

demo:

<!DOCTYPE html>
<html>
<head>
    <style>
        #navbar {
  background-color:red;
  height:200px;
  }
.navbar{
   width :220px;
}
.navbar-after-click{
    width:60px;
}
    </style>
    <script src="/scripts/snippet-javascript-console.min.js?v=1"></script>
    </head>
<body>
    <html>
  <body>
    <div id="navbar" class="navbar">
      
    </div>
    <button type="button" onclick="displayWidth();">show width </button>
  </body>
</html>
    <script type="text/javascript">
        function displayWidth(){
  var element = document.getElementById('navbar');
    style = window.getComputedStyle(element);
	alert(style.width);
  
  }
    </script>
</body>
</html>

style. //all possible objects list in below link

http://www.w3schools.com/jsref/dom_obj_style.asp

3 Comments

that's great to hear that you achieved your goal, good luck for your remaining project
can you please tell me about changes need to be done even if we put getElementsByClassName instead of getElementById
for this purpose you should add jquery and user .css() function which can get any property of element <script src="code.jquery.com/jquery-1.10.2.js"></script> <script type="text/javascript"> function displayWidth(){ alert($(".navbar").css('width')); //for class:.navbar id=#navbar } </script>
1

You can find it by jQuery.

$("#navbar").css("width")

1 Comment

sorry,i'm not using jquery i need an answer with java script
1

Here is the solution.

     function displayWidth(){

       var style = window.getComputedStyle(document.getElementById("navbar"),  null);
       var width= style.getPropertyValue("width");
       console.log(width);

     }

Comments

0

Using offsetWidth to get the width

<!DOCTYPE html>

	<html>
	<style>
		#navbar {
			background-color:red;
			height:200px;
		}
		.navbar{
			width :220px;
		}
		.navbar-after-click{
			width:60px;
		}
	</style>
	<body>
		<div id="navbar" class="navbar">

		</div>
		<button type="button" onclick="displayWidth();">show width </button>
	</body>


	<script type="text/javascript">
		function displayWidth(){

			document.getElementById("navbar").className = "navbar-after-click";
			alert(document.getElementById("navbar").offsetWidth);

		}
	</script>
	</html>

1 Comment

yes, as the class navbar is getting replaced by navbar-after-click when the button is clicked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.