I need to be able to adjust a width of a class based on a width of a parent. Currently .myClass has a width assigned in CSS. Would something like this work:
.myClass {
color: #000;
width:100px;
}
$(".myClass").width($(".myClass").parent().width());
This will be applied to a <DIV> and the parent could be a <TD> or another <DIV>.
What you have should work but I would probably change it to this:
You might be able to use
thisinstead of the second$(".myClass"), but you’d have to test that.Keep in mind that it’s not changing the class itself. It’s changing the width of any element that uses that class.
UPDATE:
If you are going to be doing any sort of calculations with the parent width then you should probably stick with your original method. I like
csswhen you are applying styles “as is” but that is a personal preference. If you are doing any kind of modifications to the parent value thenwidthis probably better.From the width documentation: