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?
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 .
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”.
var isInputText = obj instanceof HTMLInputElement && obj.type == 'text';
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With