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?

Answers


var isInputText = obj instanceof HTMLInputElement && obj.type == 'text';


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');
}

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
}

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');
}

if(textbox instanceof HTMLInputElement && textbox.getAttribute("type") == "text") { alert("I'm an input text element"); }


Need Your Help

CSS missing in Firefox but not Chrome

css google-chrome firefox

Having a problem with this block of CSS:

PDO: Invalid parameter number

php mysql pdo

I'm making an IS and I have a problem with updating mysql table. I'm using PHP 5.3 and PDO.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.