-1

I am using Spring MVC and we are saving some styles directly in our database. There is no CSS file for these styles. Now my problem is that I have styling text like below:

.className { 
    background-color: red; color: blue;
} 
.anotherClassName { 
    padding: 15px; margin: 10px;
} 

I am aware of jQuery .css method but its syntax is css("propertyname","value");

How can use my styling from jQuery?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Eatsam ul haq
  • 317
  • 1
  • 12
  • 1
    What does your `//styling` look like? Without that - at a minimum - we can't help. Where's your (relevant-only) "*[mcve]*" code? – David Thomas Oct 27 '22 at 13:33
  • @DavidThomas I just updated my question. Please let me know if there is some thing else confusing – Eatsam ul haq Oct 27 '22 at 13:43
  • 3
    This "styling text" is just CSS. Insert that in between a `` tag in your `` and all elements with those classes should have those styles. No jQuery necessary. – Rick Oct 27 '22 at 13:45
  • It's a little unclear, but are you looking for the [.addClass()](https://api.jquery.com/addclass/) method? e.g. `$("this").addClass(".anotherClassName")` – freedomn-m Oct 27 '22 at 14:12
  • Does this answer your question? [Create a CSS rule / class with jQuery at runtime](https://stackoverflow.com/q/1212500/1264804) – isherwood Oct 27 '22 at 14:23
  • @Rick thank you so much for the answer. Your answer did helped me but I think the accepted answer is better. – Eatsam ul haq Oct 27 '22 at 14:58

1 Answers1

1

As simple as this...

const myCssString = `.className { 
    background-color: red; color: blue;
} 
.anotherClassName { 
    padding: 15px; margin: 10px;
}`;

$('<style>', {
  text: myCssString
}).appendTo('head');
Levi Cole
  • 3,561
  • 1
  • 21
  • 36
  • Amazing answer sir. Thank you so much for your help. `Rick's comment` was also working but this answer seems better. Although I do want to know, where do you get answers of these question. – Eatsam ul haq Oct 27 '22 at 14:57