Home
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script> <script> $(document).ready(function () { $('#btnDivStyle').click(function(){ alert($('div').attr('class')); }); $('#btnPStyle').click(function(){ alert($('p').attr('style')); }); $('div').attr('class','yellowDiv'); // adds class='yellowDiv' to each div element }); </script> <style> .yellowDiv{ background-color:yellow; } </style> </head> <body> <h1>Demo: jQuery attr() method</h1> <button id="btnDivStyle">Get div class</button> <button id="btnPStyle">Get p Style</button> </br></br> <div> This is div. </div> <p style="font-size:16px;font-weight:bold"> This is paragraph. </p> <div> This is div. </div> <p style="color:red"> This is paragraph. </p> </body> </html>
Result: