I recently came across this scenario where I wanted to add css property to a class instead of an element.
When i do this
$('.class_with_css').css({display:'none'});
It would add the style "display:none"
to all the elements that has the class "class_with_css"
currently.
But in my case I had to apply "class_with_css"
class to a new element after the above code was executed and want to retain this style addition. Is there a way to do this (something like add the property to the css class itself) without recalling the above function?
Eg.
- two elements
<div id=1 class="abc" ></div>
<div id=2 class="abc" ></div>
- Run the code
$('.abc').css({display:'none'});
- The element becomes:
<div id=1 class="abc" style="display: none;" ></div>
<div id=2 class="abc" style="display: none;" ></div>
- Now i add class abc to element like this
<div id=3 class="abc" ></div>
Is there a way to make class "abc" to hold the style instead of element so that step 4's element also has display:none