Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - determine if input element is textbox or select list

How would I determine whether the element returned by an :input filter in jQuery is a textbox or select list?

I want to have a different behavior for each ( textbox returns text value, select returns both key and text)

Example setup:

<div id="InputBody"> <div class="box">     <span id="StartDate">         <input type="text" id="control1">     </span>     <span id="Result">         <input type="text" id="control2">     </span>     <span id="SelectList">         <select>             <option value="1">Option 1</option>             <option value="2">Option 2</option>             <option value="3">Option 3</option>         </select>     </span> </div> <div class="box">     <span id="StartDate">         <input type="text" id="control1">     </span>     <span id="Result">         <input type="text" id="control2">     </span>     <span id="SelectList">         <select>             <option value="1">Option 1</option>             <option value="2">Option 2</option>             <option value="3">Option 3</option>         </select>     </span> </div> 

and then the script:

$('#InputBody')     // find all div containers with class = "box"     .find('.box')     .each(function () {         console.log("child: " + this.id);          // find all spans within the div who have an id attribute set (represents controls we want to capture)         $(this).find('span[id]')         .each(function () {             console.log("span: " + this.id);              var ctrl = $(this).find(':input:visible:first');              console.log(this.id + " = " + ctrl.val());             console.log(this.id + " SelectedText = " + ctrl.find(':selected').text());          }); 
like image 861
ajberry Avatar asked Feb 24 '11 17:02

ajberry


People also ask

How do you know if an element is selected?

Use the tagName property to check if an element is a select dropdown, e.g. if (select. tagName === 'SELECT') {} . The tagName property returns the tag name of the element on which it was accessed. Note that the property returns tag names of DOM elements in uppercase.

How can check input value or not in jQuery?

Answer: Use the jQuery val() Method You can use the val() method to test or check if inputs are empty in jQuery. The following example will add a red outline around the inputs if it is focused but not filled out.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


2 Answers

You could do this:

if( ctrl[0].nodeName.toLowerCase() === 'input' ) {     // it was an input } 

or this, which is slower, but shorter and cleaner:

if( ctrl.is('input') ) {     // it was an input } 

If you want to be more specific, you can test the type:

if( ctrl.is('input:text') ) {     // it was an input } 
like image 190
user113716 Avatar answered Sep 21 '22 13:09

user113716


alternatively you can retrieve DOM properties with .prop

here is sample code for select box

if( ctrl.prop('type') == 'select-one' ) { // for single select }  if( ctrl.prop('type') == 'select-multiple' ) { // for multi select } 

for textbox

  if( ctrl.prop('type') == 'text' ) { // for text box } 
like image 42
Rohit Avatar answered Sep 21 '22 13:09

Rohit