I try to learn javascript and ajax, so I write any script where I want to change style of given element in form. I've trying to do this since 2 days but without any effect - I still have error "Uncaught TypeError: Cannot read property 'style' of undefined". I include my html and js code below. Please tell me what I'm doing wrong:
html:
<div id="registerNewUser" class="tabContent">
<h1>Registration form</h1>
<form action='#' name='registerForm' id='registerForm'>
<table style='width: 25%;'>
<tr>
<td>Name:</td><td><input type="text" style="border: 1px solid #000;" id='name' name='name' size ="10" onClick='changeStyle(this.id);' onBlur='checkFieldName();'></td>
<td><div id='divName'></div></td>
</tr>
<tr>
<td>Second Name:</td><td><input type="text" style="border: 1px solid #000;" id='secondName' name='secondName' size="10" onClick='changeStyle(this.id);' onBlur='checkFieldSecondName();'></td>
<td><div id='divSecondName'></div></td>
</tr>
<tr>
<td>Password:<sup style='cursor: help' title="Password must have at least 6 characters">*</sup></td><td><input type="password" style="border: 1px solid #000;" id='password' name='password' size="10" onClick='changeStyle(this.id);' onBlur='checkFieldPassword();'></td>
<td><div id='divPassword'></div></td>
</tr>
<tr>
<td>Retype Password:<sup style=' cursor: help' title="Password must have at least 6 characters">*</sup></td><td><input type="password" style="border: 1px solid #000;" name="retypePassword" id='retypePassword' size="10" onClick='changeStyle(this.id);' onBlur='checkFieldRetypePassword();'></td>
<td><div id='divRetypePassword'></div></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" style="border: 1px solid #000;" id='email' name='email' size="10" onClick='changeStyle(this.id);' onBlur='checkFieldEmail();'></td>
<td><div id='divEmail'></div></td>
</tr>
</table>
</form>
</div>
javascript:
var registerForm=document.getElementById('registerForm'); // register form
function changeStyle(element)
{
registerForm.element.style.border='1px solid #fff';
}
Thanks in advance.
The “cannot read property of undefined” error occurs when you attempt to access a property or method of a variable that is undefined . You can fix it by adding an undefined check on the variable before accessing it.
The "Cannot read property 'value' of null" error occurs when: trying to access the value property on a null value, e.g. after calling getElementById with an invalid identifier. inserting the JS script tag before the DOM elements have been declared.
The other common reason the error occurs is placing the JS script tag above the code that declares the DOM elements. To solve the "Cannot read property 'style' of undefined" error, make sure that the JS script tag is placed at the bottom of the body, after the HTML elements have been declared.
To resolve the "Cannot read property 'style' of null" error, make sure that the DOM element you're accessing the style property on exists. In the example, an element with the provided id does not exist in the DOM, so the getElementById method returns null .
You need to get the element by ID:
document.getElementById(element).style.border = '1px solid #fff';
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