Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

Am able to change the background of a div to blue with the below code using jquery.

$('#infoline').css('background-color', 'blue');

But not able to set it to to one of the bootstrap colors like the below code
$('#infoline').css('background-color', bg-primary);

Please advise the right way to do this.

What I have tried:

Tried even the below syntax :

$('#infoline').css('background-color', 'bg-primary');


Searched on google. Not able to get the right phrase for the relevant links.
Updated 29-Nov-20 20:08pm

1 solution

You need to play at class level for using bootstrap colors like:
$("p").addClass("bg-warning text-white");

More details: jQuery Get and Set CSS Classes[^]

A working sample:
<!DOCTYPE html>
<html lang="en">
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <script src=""></script>
    $("p").addClass("bg-warning text-white");

<div class="container">
  <button>Set the color property of all p elements</button>
  <p>This is a paragraph.</p>

Share this answer
Priya-Kiko 30-Nov-20 2:12am    
Thank you. Got it !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900