tags:

views:

41

answers:

4

Hi

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 its a checkbox or radio button it doesnt have a length(no strings in it) otherwise if its 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 find the type of input, then determine what to do.

Any suggestions would be appreciated.

Thanks in advance.

+4  A: 

Check the type property. Would that suffice?

troelskn
use it like element.type, it would return textarea it the element is a textarea and so on.
anand
A: 
var vartype = typeof(variable);
danp
Not what the OP is looking for... Although the question title is misleading
Pekka
yes,but its mentioned clearly in the description though..
anand
A: 

I'm so far to understand your question. If you wanna heck the type of input within form, here is coding:

<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.")
            }
            if (obj.childNodes[i].type == "checkbox") {
               alert("this is CheckBox.")
            }
            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>
ppshein
You don't have to use those comments anymore: http://dorward.me.uk/www/comments-cdata/
Marcel Korpel
A: 

If you are using jQuery you can easily check the type of any element.

function(elementID){    
var type = $(elementId).attr('type');
if(type == "text") //inputBox
 console.log("input text" + $(elementId).val().size());

}

similarly you can check the other types and take appropriate action.

kapser
Or, if you use Javascript, you could write `document.getElementById(elementId).type`. I fail to see the benefit that jQuery gives in this case.
troelskn