I am hoping you guys can help me with this. I have the following code
</textarea>
<br />Word Count: <input type="text" name="c" value="311" size="5" onkeyup="cnt(document.script.w,this)" />
</form>
<script type="text/javascript">
var myNumValue = document.getElementById('c').value;
var myNum = parseInt(myNumValue);
var upperLimit=200;
var lowerLimit=10;
if(upperLimit == lowerLimit)
{
document.getElementById('div1').style.visibility='visible';
}
</script>
&l开发者_StackOverflowt;div id="div1" style="visibility: hidden;">
Super cool hidden div!
</div>
For some reason, I cannot get the div to show. I have tried setting the upper and lower the same and using == in if, anything inside of the if displays (for example if a do a document.write it will show up) but for some reason the div will not show.
What am I missing?
Thanks!
Kevin
Your first problem is that the input has no id
so, as Sarfraz says, document.getElementById('c')
will error.
That is not the only critical problem though.
Your script is not in a function, so it will run as soon as it is parsed.
The div element appears in the document after the script element, so the browser doesn't know it exists at the time the script runs.
document.getElementById('div1')
will therefore error.
Either move the script element to after the div element, or wrap it in a function and delay execution until an event that will fire after the element exists (such as onload (standard) or ondomready (provided by many JS libraries).
You have not specified id
attribute for your field:
<input type="text" name="c" value="311" size="5" onkeyup="cnt(document.script.w,this)" />
Should be:
<input type="text" name="c" id="c" value="311" size="5" onkeyup="cnt(document.script.w,this)" />
Because you are missing the id
there, you are most likely getting an error and your script halts in the middle; a reason why your code does not reach that if
condition.
Did you forget to close your <script>
tag or did you just not cut/paste it?
The line
if(upperLimit == lowerLimit)
is never true; you just defined upperLimit to be not equal to lowerLimit.
Please try this
document.getElementById('div1').style.display = 'block';
u have not created object of div1 first create object.
<div id="div1" style="visibility: hidden;">
Super cool hidden div!
</div>
<script type="text/javascript">
var myNumValue = document.getElementById('c').value;
var myNum = parseInt(myNumValue);
var upperLimit=200;
var lowerLimit=200;
if(upperLimit == lowerLimit)
{
document.getElementById('div1').style.visibility='visible';
}
</script>
精彩评论