I encountered a situation where I needed to apply a css property to a class rather than an element.
For example:
$('.class_with_css').css({display:'none'});
This code would add the style "display:none"
to all elements with the class "class_with_css"
. However, I wanted to apply the "class_with_css"
class to a new element after this and keep the added style. Is there a way to achieve this without calling the above function again?
For instance:
- Initial setup with two elements
<div id=1 class="abc" ></div>
<div id=2 class="abc" ></div>
- Run the code
$('.abc').css({display:'none'});
- The elements become:
<div id=1 class="abc" style="display: none;" ></div>
<div id=2 class="abc" style="display: none;" ></div>
- Add the "abc" class to a new element like this
<div id=3 class="abc" ></div>
Is there a way to make the "abc" class hold the style instead of individual elements so that even the element in step 4 also has display:none
?