Forum Moderators: open

Message Too Old, No Replies

Data type of object style property value

         

Rain_Lover

7:02 am on Dec 17, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Consider this:

main {
width: 400px;
height: 100px;
border: 1px solid #C3C3C3;
display: flex;
}
main div {
flex-grow: 1;
}

<main>
<div style="background-color:coral;"></div>
<div style="background-color:lightblue;"></div>
<div style="background-color:khaki;" id="khaki"></div>
<div style="background-color:pink;"></div>
<div style="background-color:lightgrey;"></div>
</main>
<input type="number" id="input" value="9" max="9">
<span id="dataType"></span>

var input = document.getElementById('input'),
khaki = document.getElementById('khaki'),
dataType = document.getElementById('dataType');
input.oninput = function() {
khaki.style.flexGrow = 10 - this.value;
dataType.textContent = 'Data type: ' + typeof khaki.style.flexGrow;
};


DEMO [jsfiddle.net]

I wonder why it shows the data type of the flex-row property value as string, but if you replace khaki.style.flexGrow with its equivalent 10 - this.value:

dataType.textContent = 'Data type: ' + typeof (10 - this.value);


it will show the data type as number.
Can I generalize it and conclude that all objects style property values and all elements attribute values are strings?

Fotiman

2:29 pm on Dec 17, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That is correct. All element attribute values are represented as strings when fetched with JavaScript.

Rain_Lover

3:29 pm on Dec 17, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for the confirmation!