Page 1 of 1
Javascript question
Posted: Thu Sep 01, 2005 9:42 am
by Eraser
Does anyone know if it's possible to determine what sort of object an object is? I hook the object with getElementById:
myObject = document.getElementById("nameofelement");
Then I want to know whether myObject is an <input> element or <textarea> element. Is this possible? And if so, how?
Posted: Thu Sep 01, 2005 10:12 am
by MKJ
do you want "nameoflelement" to be whatever the myObject is ?
Posted: Thu Sep 01, 2005 11:19 am
by Eraser
MKJ wrote:do you want "nameoflelement" to be whatever the myObject is ?
myObject is simply the <input> element or <textarea> element in my HTML. However, I don't know if myObject is <input> or <textarea>, and that is what I wish to find out.
Posted: Thu Sep 01, 2005 11:40 am
by 4days
.type?
Code: Select all
for (var i = 0; i < document.myForm.elements.length; i++) {
document.writeln("<br />type is " + document.myForm.elements[i].type)
}
Posted: Thu Sep 01, 2005 1:26 pm
by Eraser
hmm that seems to return "text" with both <input type="text"> and <textarea> tags
Posted: Thu Sep 01, 2005 2:03 pm
by 4days
sorry, missed the rest of your post. you'll have to specify some other detail about the textarea elements. is there a reason you can't give the form elements ids or classes, or grab them by name?
Posted: Thu Sep 01, 2005 5:29 pm
by neh
you could test for null on its maxlength property - textarea doesnt have one so the ref returned should be null
Posted: Thu Sep 01, 2005 5:30 pm
by neh
lol - actually you'd need to brute force that check in a try catch block