Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if object is a textbox - javascript

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?

like image 628
simplified. Avatar asked Jun 22 '11 18:06

simplified.


People also ask

How do I check if a DOM element is input?

We can use the tagName property and type attribute of the DOM element to check if the DOM element is input or not. For the input DOM element, the tagName is INPUT .

How check input value is number or not in JavaScript?

In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.


2 Answers

var isInputText = obj instanceof HTMLInputElement && obj.type == 'text'; 
like image 187
Mick Owen Avatar answered Oct 14 '22 01:10

Mick Owen


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; } 
like image 42
Onur Yıldırım Avatar answered Oct 14 '22 01:10

Onur Yıldırım