css() method sets or returns one or more style properties for the selected elements. - Javascript jQuery

Javascript examples for jQuery:CSS

Introduction

The following example will return the background-color value of the FIRST matched element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        console.log("Background color = " + $("p").css("background-color"));
    });//w w w  .ja va  2 s.  c  om
});
</script>
</head>
<body>

<h2>This is a heading</h2>

<p style="background-color:#ff0000">This is a paragraph.</p>
<p style="background-color:#00ff00">This is a paragraph.</p>
<p style="background-color:#0000ff">This is a paragraph.</p>

<button>Return background-color of p</button>

</body>
</html>

Related Tutorials