I'm getting a javascript error from my console that reads:
Uncaught TypeError: Cannot read property 'style' of null
Does anyone know whats wrong?
Code (JS):
<script>
function select()
{
document.getElementById('#iframetest').style.display='block';
}
</script>
Code (HTML):
<iframe src="localhost/createaclass" id="iframetest"></iframe>
<div class="b" onclick="select()">
I'm getting a javascript error from my console that reads:
Uncaught TypeError: Cannot read property 'style' of null
Does anyone know whats wrong?
Code (JS):
<script>
function select()
{
document.getElementById('#iframetest').style.display='block';
}
</script>
Code (HTML):
<iframe src="localhost/createaclass" id="iframetest"></iframe>
<div class="b" onclick="select()">
Share
Improve this question
edited Aug 11, 2014 at 3:14
nstCactus
5,1832 gold badges33 silver badges42 bronze badges
asked Aug 11, 2014 at 3:10
user3818791user3818791
751 gold badge1 silver badge9 bronze badges
2
- there is no jQuery used in this code... – Arun P Johny Commented Aug 11, 2014 at 3:13
-
1
the
#
is used to specify a id selector, butdocument.getElementById()
does not accept a selector, it expects an id to be passed to it – Arun P Johny Commented Aug 11, 2014 at 3:14
4 Answers
Reset to default 9Don't place the hash in id (#):
document.getElementById('iframetest')
As per your ment, you can do like this:
function select()
{
document.getElementById('iframetest').style.display = 'block' ? 'none' : 'block';
}
Move your JS below the HTML.
<iframe src="localhost/createaclass" id="iframetest"></iframe>
<div class="b" onclick="select()">
<script>
function select()
{
document.getElementById('#iframetest').style.display='block';
}
</script>
Maybe too late to answer it. But your selector is returning null because at this line
document.getElementById('#iframetest').style.display='block';
You need to change it to:
document.getElementById('iframetest').style.display='block';
When you specify getElementById, then your JS will directly go to the element with the id of 'iframetest'. There is no need of '#'.
try this if you are using jquery. it's working.
$('.b').click(function(){
$('#iframetest').hide();
});
<iframe src="localhost/createaclass" id="iframetest"></iframe>
<div class="b">selectDiv</div>
see jsfiddle link http://jsfiddle/17duuxcm/5/