I’m implementing a length counter on textareas for a website (twitter style). I’m using the code found here http://www.codefromjames.com/wordpress/?p=15 but seems not to work in IE7.
I’ve traced the issue to the check of the attribute “maxlength” in an element. It expects it to be null if the attribute is not found, but IE7 returns a number (which I guess is the maximum length allowed by the browser or by the HTML standard, I don’t know).
Here’s an example you can try:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
</head>
<body>
<input id="test"></input>
</body>
<script type="text/javascript">
var element = document.getElementById("test");
alert(element.getAttribute("maxlength"));
</script>
</html>
Try executing this on IE7 and you’ll find out it gives a number instead of null.
Exact version of IE7 is 7.0.5703.13
Should I consider this a IE7 bug or is it working as expected?
Cheers!
You can check
element.attributes.maxLength.specifiedto find out whether maxLength is indeed user-specified.