This question might be a duplicate or stupid. I've read questions kind of similar to this but not this exact thing.
Suppose I have two dynamic generated li
's with classes of .someclass
and .someclass2
and both have different widths that needs to be calculated on the page load.
So, is there a way to set the calculated width to the classes once instead of setting the width every time a new li
is generated?
We can do this with css, If the width is known, .someclass { width: 20px;} .someclass2 { width: 50px;}
and then whenever a new element generates the css gets applied to them.
Applying width every time there's a append happening
$('#somediv').click(somefunction);
function somefunction() {
$('#somediv').append('<li class="someclass"><li class="someclass2"></li></li>');
$('.someclass').css('width', $(document).width() - 112); //every time
$('.someclass2').css('width', $(document).width() - 250); //every time
}
Applying width once - Is there a way to do it only once?
$('.someclass').css('width', $(document).width() - 112); //once, but it will not work
$('.someclass2').css('width', $(document).width() - 250); //once, but it will not work
$('#somediv').click(somefunction);
function somefunction() {
$('#somediv').append('<li class="someclass"><li class="someclass2"></li></li>');
}
What if there are way more elements and it happens frequently, would there be any performance difference?