1

I need to convert following lines of vbscript codes to javascripts, If TypeName(document.all(cFieldName)) = "HTMLInputElement" Then and

ElseIf TypeName(document.all(cFieldName)) = "HTMLSelectElement" Then

I have tried if(typeof $("#" + (cFieldName)) === "HTMLInputElement") and else if(typeof $("#" + cFieldName) === "HTMLSelectElement") but they are not working.

1

1 Answer 1

1

In this specific case, you can do this:

if (document.all[cFieldName].nodeName === "INPUT") {
    // It's an input element
}

and

if (document.all[cFieldName].nodeName === "SELECT") {
    // It's a select element
}

(That assumes HTML, rather than XHTML. In XHTML, the names will be lower case. To avoid messing myself up, I usually throw .toUpperCase() in there.)

As you've discovered, typeof will just give you "object". It's possible that on some engines, Object.prototype.toString.call(document.all[cFieldName]) might give you "HTMLInputElement", but I wouldn't expect it to be reliable cross-browser (and I assume one reason this code is being translated is to run it on browsers other than IE). There's more on various ways of figuring out what things are in JavaScript (though not specifically in regard to HTML elements) in my blog post Say what?.

Sign up to request clarification or add additional context in comments.

2 Comments

Instead of .all you could use $("#selector").get(0).nodeName
@SalmanA: Or even better, $("#selector")[0].nodeName (although document.all on IE has things in it by their name as well as by their id so an id selector may not be the right thing). I have to admit I didn't even notice the jquery tag and was just focussing on the typeof aspect. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.