In my app, I'm displaying data coming from a database. Attached to the data are metadata that define how it should be displayed. To keep it simple, let's assume the only metadata is the size of the square it should be displayed in. For example :
dataArray : {
{squareSize: "squareSize1", value:"foo"},
{squareSize: "squareSize3", value:"bar"},
{squareSize: "squareSize4", value:"oof"},
}
HTML :
<div id="dataGrid" class="grid">
</div>
CSS:
.squareSize1 { height: 100px; }
.squareSize2 { height: 200px; }
.squareSize3 { height: 300px; }
.squareSize4 { height: 400px; }
JAVASCRIPT :
document.ready(function() {
// ... //
//
// {squareSize : "squareSize4", value: "foo"}
dataArray.forEach((data, index) => {
let html = "<div id=" + index
html += " class=\"" + data.squareSize + "\" >"
html += data.value + "</div>"
$dataGrid[0].innerHTML += html;
// logs the height of the div
// i.e. if data was of type "squareSize4" : 400
console.log($("." + data.squareSize).height());
});
}
Later in the code (not in the document.ready()
I have a way for users to add content from the same kind of data.
Problem is, if an element of the same css class doesn't already exist, I can't get the height :
// I have elements of classe squareSize1 and squareSize3 :
console.log($(".squareSize1").height()); // 100
console.log($(".squareSize2").height()); // undefined
console.log($(".squareSize3").height()); // 300
Same results with .css('height')
:
// I have elements of classe squareSize1 and squareSize3 :
console.log($(".squareSize1").css('height')); // 100px
console.log($(".squareSize2").css('height')); // undefined
console.log($(".squareSize3").css('height')); // 300px
QUESTION : Is it possible to get this value of 200 form my css if I don't have any element of sqaureSize2 in my dom yet ?
P.s. I need this value to do some advanced UI stuff