-1

I have div in my html. On a button click event, I want to get all style attribute at once, so that I can use it for another task

    <div id=popUp style="display: block; z-index: 1050; top: 371px; left: 847.75px;">
        </div>
    <button id="btn" value="submit">

$('#btn').on('click', function (event){
           var styleAttribute=$('#popUp').css('style');
console.log('styleAttribute',styleAttribute);
        });

It gives undefined. What I want is this as console.

display: block; z-index: 1050; top: 371px; left: 847.75px;
Vikas Gupta
  • 1,183
  • 1
  • 10
  • 25

3 Answers3

1

Use jQuery#attr function to the attribute style instead of the jQuery#css - which returns the properties of the style.

For individual style properties you can use jQuery#css, passing the property name, for example .css('display') and get the value of the display property.

$('#btn').click(function(){
   var styleAttribute = $('#popUp').attr('style');
   console.log('styleAttribute ', styleAttribute);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="popUp" style="display: block; z-index: 1050; top: 371px; left: 847.75px">
</div>
<button id="btn" value="submit">Get style</button>
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

Call with attr()

$('#btn').click(function(){
var styleAttribute=$('#popUp').attr('style');
console.log('styleAttribute',styleAttribute);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=popUp style="display: block; z-index: 1050; top: 371px; left: 847.75px;">
        </div>
    <button id="btn" value="submit">click</button>
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

Use attr.

$("#btn").on("click", function() {
  var styleAttribute = $('#popUp').attr('style');
  console.log(styleAttribute);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=popUp style="display: block; z-index: 1050; top: 371px; left: 847.75px;">
</div>
<button id="btn" value="submit">
4b0
  • 21,981
  • 30
  • 95
  • 142