Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery - determine if input element is textbox or select list
    primarykey
    data
    text
    <p>How would I determine whether the element returned by an :input filter in jQuery is a textbox or select list?</p> <p>I want to have a different behavior for each ( textbox returns text value, select returns both key and text)</p> <p>Example setup:</p> <pre><code>&lt;div id="InputBody"&gt; &lt;div class="box"&gt; &lt;span id="StartDate"&gt; &lt;input type="text" id="control1"&gt; &lt;/span&gt; &lt;span id="Result"&gt; &lt;input type="text" id="control2"&gt; &lt;/span&gt; &lt;span id="SelectList"&gt; &lt;select&gt; &lt;option value="1"&gt;Option 1&lt;/option&gt; &lt;option value="2"&gt;Option 2&lt;/option&gt; &lt;option value="3"&gt;Option 3&lt;/option&gt; &lt;/select&gt; &lt;/span&gt; &lt;/div&gt; &lt;div class="box"&gt; &lt;span id="StartDate"&gt; &lt;input type="text" id="control1"&gt; &lt;/span&gt; &lt;span id="Result"&gt; &lt;input type="text" id="control2"&gt; &lt;/span&gt; &lt;span id="SelectList"&gt; &lt;select&gt; &lt;option value="1"&gt;Option 1&lt;/option&gt; &lt;option value="2"&gt;Option 2&lt;/option&gt; &lt;option value="3"&gt;Option 3&lt;/option&gt; &lt;/select&gt; &lt;/span&gt; &lt;/div&gt; </code></pre> <p></p> <p>and then the script:</p> <pre><code>$('#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()); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload