function test( id )
{
alert( document.getElementById( id ).style.display );
}
What exactly does getElementById.style.display return? Is it an object, or a value? The alert shows nothing at all. I'm not using pure numbers for the id and it is unique.
getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.
To show an element, set the style display property to “block”. document. getElementById("element").
You can use the getElementById() method as many times as you wish, there's no quota assigned to that method.
DOM methods like document.getElementById()
create objects which point to- and contains certain details about- a specified HTMLElement or set of elements.
Unfortunately the .style
property only knows about the style properties set using that same feature. In the example below, clicking what color?
will not work until you have clicked change color
.
<html>
<head>
<script>
function whatColor() {
var d = document.getElementById( 'ddd' );
alert( d.style.backgroundColor );
}
function changeColor() {
var d = document.getElementById( 'ddd' );
d.style.backgroundColor='orange';
}
</script>
<style>
#ddd {
background-color:gray;
}
</style>
</head>
<body>
<input type="button" onclick="whatColor();" value="what color?" />
<input type="button" onclick="changeColor();" value="change color" />
<div id="ddd"> </div>
</body>
</html>
I recommend reading PKK's great page on getComputedStyle and currentStyle (IE, of course is different) http://www.quirksmode.org/dom/getstyles.html
At the end of the tutorial, there is a very decent function for your purposes, although frameworks such as jQuery do provide very convenient & powerful functions for styling page elements: http://api.jquery.com/css/
it displays the value that was dynamically set. if you want to find the current value you need the computed value. the same question was asked, check my answer here
Stackoverflow
Actually it is possible using window.getComputedStyle(element[, pseudoElt])
.
The method gives the values of all the CSS properties of an element after applying the active stylesheets and resolving any basic computation those values may contain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With