css() in javascript code example

Example 1: jquery add style

//revising Ankur's answer
//Syntax:
$(selector).css({property-name:property-value});

//Example:
$('.bodytext').css({'color':'red'});

Example 2: set css using jquery

$('.name').css('color':'blue');

Example 3: edit css jquery

$('.ama').css('color','red');

Example 4: how to give css style in javascript

document.getElementById("myH1").style.color = "red";

Example 5: change color of css in js

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Change the Background Color with JavaScript</title>
<script>
    // Function to change webpage background color
    function changeBodyBg(color){
        document.body.style.background = color;
    }
    
    // Function to change heading background color
    function changeHeadingBg(color){
        document.getElementById("heading").style.background = color;
    }
</script>
</head>
<body>
    <h1 id="heading">This is a heading</h1>
    <p>This is a paragraph of text.</p>
    <hr>
    <div>
        <label>Change Webpage Background To:</label>
        <button type="button" onclick="changeBodyBg('yellow');">Yellow</button>
        <button type="button" onclick="changeBodyBg('lime');">Lime</button>
        <button type="button" onclick="changeBodyBg('orange');">Orange</button>
    </div>
    <br>
    <div>
        <label>Change Heading Background To:</label>
        <button type="button" onclick="changeHeadingBg('red');">Red</button>
        <button type="button" onclick="changeHeadingBg('green');">Green</button>
        <button type="button" onclick="changeHeadingBg('blue');">Blue</button>
    </div>
</body>
</html>

Example 6: how to css in jquery

$(init);
    
function init() {
    $("h1").css("backgroundColor", "yellow");
    $("#myParagraph").css({ "backgroundColor": "black", "color": "white" });
    $(".bordered").css("border", "1px solid black");
}

Tags:

Css Example