snipt

Ctrl+h for KB shortcuts

JavaScript

JQuery Get Combo Value

1
2
3
4
$("#ComboBox").val();
$("#ComboBox option:selected").text(); //get the text of the selection
$("#ComboBox").val(2); //set selected item
$("#ComboBox").change(function() { /* do something here */ });
https://snipt.net/embed/12e5fc1babcb9c2fa94d2a84d4bcd042/
/raw/12e5fc1babcb9c2fa94d2a84d4bcd042/
12e5fc1babcb9c2fa94d2a84d4bcd042
js
JavaScript
5
2019-06-15T22:18:43
True
False
False
Oct 08, 2013 at 07:06 PM
/api/public/snipt/18123/
jquery-get-combo-value
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#ComboBox&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">();</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#ComboBox option:selected&quot;</span><span class="p">).</span><span class="nx">text</span><span class="p">();</span> <span class="c1">//get the text of the selection</span> </span><span id="L-3"><a name="L-3"></a><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#ComboBox&quot;</span><span class="p">).</span><span class="nx">val</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span> <span class="c1">//set selected item</span> </span><span id="L-4"><a name="L-4"></a><span class="nx">$</span><span class="p">(</span><span class="s2">&quot;#ComboBox&quot;</span><span class="p">).</span><span class="nx">change</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span> <span class="cm">/* do something here */</span> <span class="p">});</span> </span></pre></div> </td></tr></table>
combobox, javascript, jquery