Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML form control check?

Status
Not open for further replies.

g2000

Programmer
Aug 31, 2005
39
US
Is that possible (using javascript) to check if any input element belongs to certain type such as textbox or select box?
 
Code:
var obj = document.forms['myForm'].elements['myElement'];

switch( obj.type ) {
  case 'text':
    alert('text box');
    break;
  case 'select-one':
    alert('single select');
    break;
  default:
    alert('other');
    break;
}

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Yes. First, either grab the specific input element or all the elements.
/* For a specific element. Search by its id property.
var input=document.getElementById("element_id");
OR
// For an array of all elements in your document.
var inputs=document.getElementsByTagName("input");

For the first example, you can see its type by:
alert(input.type);
if (input.type=="textbox") ....

For the second example, you can see its type by:
alert(inputs[0].type);
Of course, you pick whichever index you need or simply loop through them all.

Lastly, a select box is not an input. It is a select tag.


 
True, select is not an input, but it still has a type property. Which brings up a good point: the reference I gave above only shows the types for input tags, but there are other tags that have types too. The MSDN DHTML Reference has type property entries for each of the following:

A, LINK, OBJECT
BUTTON
event
INPUT
LI, OL, UL
PARAM
SCRIPT
SELECT
selection
STYLE
styleSheet
TEXTAREA

To check them out go to MDSN DHTML Reference - Properties and scroll down to "type".

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top