Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the 'type' of an input element

Tags:

javascript

I'd like to be able to find the type of something on the page using JavaScript. The problem is as follows: I need to check whether a specific area is a checkbox/radio button/ or text field.

If it's a checkbox or radio button it doesn't have a length (no strings in it), otherwise if it's a textfield I need to check whether or not it contains characters. The page is created dynamically so sometimes a checkbox may be displayed, other times a text field.

So my thinking is to find the type of the input, then determine what to do.

Any suggestions would be appreciated.

like image 335
Julio Avatar asked Aug 18 '10 09:08

Julio


People also ask

What type of element is input?

<input>: The Input (Form Input) element. The <input> HTML element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent.

What are the elements of input?

<input type="range"> <input type="reset"> <input type="search"> <input type="submit">

What is the input type of input () function?

The input() function reads a line from the input (usually from the user), converts the line into a string by removing the trailing newline, and returns it. If EOF is read, it raises an EOFError exception.


2 Answers

Check the type property. Would that suffice?

like image 114
troelskn Avatar answered Sep 23 '22 12:09

troelskn


If you want to check the type of input within form, use the following code:

<script>     function getFind(obj) {     for (i = 0; i < obj.childNodes.length; i++) {         if (obj.childNodes[i].tagName == "INPUT") {             if (obj.childNodes[i].type == "text") {                 alert("this is Text Box.")             }             else if (obj.childNodes[i].type == "checkbox") {                 alert("this is CheckBox.")             }             else if (obj.childNodes[i].type == "radio") {                 alert("this is Radio.")             }         }         if (obj.childNodes[i].tagName == "SELECT") {             alert("this is Select")         }     } } </script>       <script>         getFind(document.myform);   </script> 
like image 34
PPShein Avatar answered Sep 19 '22 12:09

PPShein