Ho can I set these properties:
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
On an element with javascript. I usually do it like so:
var element = document.getElementById("#element")
element.style.display = "flex";
But what about the rest of the properties in there, what could I do to apply those ?
Ho can I set these properties:
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
On an element with javascript. I usually do it like so:
var element = document.getElementById("#element")
element.style.display = "flex";
But what about the rest of the properties in there, what could I do to apply those ?
Share Improve this question asked Jul 27, 2013 at 10:39 RolandRoland 9,74119 gold badges82 silver badges136 bronze badges 4-
Do the same, add the same lines for each but replace
flex
for the other properties? – putvande Commented Jul 27, 2013 at 10:42 - Won't that overwrite the previous styles I just applied ? Shouldn't I get the puted styles first and then add ? – Roland Commented Jul 27, 2013 at 10:42
-
@putvande That's not a good idea, only the last one would be a value of
display
. – Teemu Commented Jul 27, 2013 at 10:44 - @Teemu ~ precisely why I asked this question ... – Roland Commented Jul 27, 2013 at 10:45
2 Answers
Reset to default 4You can develope this code snippet further:
var elem = document.getElementById('id_of_element_to_check'),
dispValue = ['-webkit-box', '-moz-box', '-ms-flexbox', '-webkit-flex', 'flex'],
n;
for (n = 0; n < dispValue.length; n++) {
elem.style.display = dispValue[n];
if (window.getComputedStyle(elem, null).display === dispValue[n]) {
break;
}
}
The idea is, that window.getComputedStyle()
won't return invalid values. The check can be also put in a while
condition, but maybe it's safest to do within a for
loop. I tested this in Chrome28, FF22 and IE10.
A live demo at jsFiddle.
What about adding in css :
.flexbox{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
And then adding a class ".flexbox" to the element like said in this topic :
How do I add a class to a given element?