37

I understand that we can use (javascript)

if (typeof textbox === "object") { } 

but are there methods which will allow me to ensure that the object is a textbox?

simplified.
  • 371
  • 1
  • 3
  • 3

6 Answers6

44
var isInputText = obj instanceof HTMLInputElement && obj.type == 'text';
Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
Mick Owen
  • 649
  • 6
  • 8
  • 1
    A word of warning: if your app is one of those rare ones that use multiple windows with cross-window DOM inspection (or similar), `window.HTMLInputElement === otherWindow.HTMLInputElement` will be false, thus the `instanceof` operator may also unexpectedly return false. Applies to iframes and popup windows as well. In those cases, checking `tagName` can work, as other answers already mentioned. – John Weisz Nov 16 '20 at 12:59
26

As of 2016, use this:

function isTextBox(element) {
    var tagName = element.tagName.toLowerCase();
    if (tagName === 'textarea') return true;
    if (tagName !== 'input') return false;
    var type = element.getAttribute('type').toLowerCase(),
        // if any of these input types is not supported by a browser, it will behave as input type text.
        inputTypes = ['text', 'password', 'number', 'email', 'tel', 'url', 'search', 'date', 'datetime', 'datetime-local', 'time', 'month', 'week']
    return inputTypes.indexOf(type) >= 0;
}
Onur Yıldırım
  • 32,327
  • 12
  • 84
  • 98
12

Are you looking for something like this?

if(textbox.tagName && textbox.tagName.toLowerCase() == "textarea") {
    alert('this is a textarea');
}

If you need to know if it's a text input, you can do this:

if(textbox.tagName && textbox.tagName.toLowerCase() == "input" && textbox.type.toLowerCase() == "text") {
    alert('this is a text input');
}
Jordan
  • 31,971
  • 6
  • 56
  • 67
  • @Sime: sometimes, it depends on the browser and whether it's in HTML or XHTML. I'll update my answer to make sure it works. – Jordan Jun 22 '11 at 19:01
  • @Sime: http://reference.sitepoint.com/javascript/Element/tagName. Opera returns it how it was typed, HTML is uppercase, XHTML is preserved. – Jordan Jun 22 '11 at 19:03
  • @Jordan Yes: *"In HTML this is returned in all uppercase..."* – Šime Vidas Jun 22 '11 at 19:04
  • @Sime, right, so if the document is XHTML, it's not guaranteed. – Jordan Jun 22 '11 at 19:05
  • Either way I edited my answer so that it takes all of that into account, regardless of doctype and browser. – Jordan Jun 22 '11 at 19:06
  • @Jordan (1) XHTML documents have the "text/html" mime-type, so browsers treat them as HTML documents, not XML documents. (2) This Opera-thing is only for namespaced elements (custom element types). – Šime Vidas Jun 22 '11 at 19:09
  • @Šime Vidas - If you deliver your XHTML as "application/xhtml+xml" it is supposed to be treated differently, noting that not all browsers actually support that. (Though in my view XHTML is a waste of time if you're just going to deliver it as "text/html".) – nnnnnn Jun 23 '11 at 00:16
  • @nnn Yes, but in IRL XHTML documents delivered as "application/xhtml-xml" don't exist (at least I haven't heard of any prominent web-server doing it), since IE doesn't have a XML parser. All web-pages on the web are "text/html", ergo, `tagName` will always be uppercase. – Šime Vidas Jun 23 '11 at 00:49
3

If it's a text input you're looking for:

if (textbox.tagName == "input" && textbox.getAttribute("type") == "text") {
   // it's a text input
}

If you're looking for a textarea

if (textbox.tagName == "textarea") {
  // it's a textarea
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
2
if(textbox instanceof HTMLInputElement && textbox.getAttribute("type") == "text") {
    alert("I'm an input text element");
}
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
vrunoa
  • 766
  • 8
  • 15
1

I think perhaps you would want to get a reference to an element, and then check for the return value of .type i.e.

var element = document.getElementById('element_in_question');
if(element.type == "textarea"){
  console.log('I must be textarea');
}
thescientist
  • 2,906
  • 1
  • 20
  • 15