Javascript question

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
User avatar
Eraser
Posts: 19181
Joined: Fri Dec 01, 2000 8:00 am

Javascript question

Post 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?
User avatar
MKJ
Posts: 32582
Joined: Fri Nov 24, 2000 8:00 am

Post by MKJ »

do you want "nameoflelement" to be whatever the myObject is ?
[url=http://profile.mygamercard.net/Emka+Jee][img]http://card.mygamercard.net/sig/Emka+Jee.jpg[/img][/url]
User avatar
Eraser
Posts: 19181
Joined: Fri Dec 01, 2000 8:00 am

Post 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.
4days
Posts: 5465
Joined: Tue Apr 16, 2002 7:00 am

Post 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)
}
User avatar
Eraser
Posts: 19181
Joined: Fri Dec 01, 2000 8:00 am

Post by Eraser »

hmm that seems to return "text" with both <input type="text"> and <textarea> tags
4days
Posts: 5465
Joined: Tue Apr 16, 2002 7:00 am

Post 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?
neh
Posts: 662
Joined: Mon Mar 12, 2001 8:00 am

Post by neh »

you could test for null on its maxlength property - textarea doesnt have one so the ref returned should be null
neh
Posts: 662
Joined: Mon Mar 12, 2001 8:00 am

Post by neh »

lol - actually you'd need to brute force that check in a try catch block
Post Reply