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.
In this specific case, you can do this:
and
(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,
typeofwill 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?.