i have few div’s in my form which needs to be hidden at first i.e onLoad. the problem is when i put that into <head> tag it will try and execute first giving out the error that div has value null, and when i include it below the form it works perfectly. for example
this code works fine given the JS code is placed below.
<div class="error" id="name_error"><p>Input Name</p></div>
<input type="text" id="name" name="name" class="input-text" value="Enter Name..."/>
<script type="text/javascript">
document.getElementById('name_error').style.display = 'none';
</script>
and when i try placing the JS code above it doesn’t work.
is there anyway i could place the JavaScript code in head tag and it should hide the div when the page loads?
You need to wrap your code in
onloadevent:This way you won’t get undefined error because the
onloadevent is triggered when DOM is available along with other page resources.