I’m stuck with getting the value attribute of an option in a select list with jQuery Mobile.
My HTML template:
<div data-role="fieldcontain" class="activity ui-hide-label">
<label for="activity">Activity</label>
<select name="activity" id="activity">
<option value="">Activity</option>
<option value="3">Athlete</option>
<option value="2">Regular sports</option>
<option value="1">No sports</option>
</select>
</div>
My JS function to get the value:
function getValue() {
var activityValue = $('#activity').val();
console.log("activity value: ", activityValue);
localStorage['activity'] = activityValue;
}
Regardless what I choose from the select list, the variable activityValue is always empty. I also tried with option:selected and similar possibilites.
Obviously the DOM select list doesn’t change, when I choose an option. For example, if I choose “Regular sports” the DOM looks like this:
<div data-role="fieldcontain" class="activity ui-hide-label ui-field-contain ui-body ui-br">
<label for="activity" class="ui-select">Activity</label>
<div class="ui-select">
<div data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-icon="arrow-d" data-iconpos="right" data-theme="g" data-inline="false" data-mini="false" class="ui-btn ui-shadow ui-btn-corner-all ui-fullsize ui-btn-block ui-btn-icon-right ui-btn-up-g">
<span class="ui-btn-inner ui-btn-corner-all">
<span class="ui-btn-text">Regular sports</span>
<span class="ui-icon ui-icon-arrow-d ui-icon-shadow"> </span>
</span>
<select name="activity" id="activity">
<option value="">Activity</option>
<option value="1">Athlete</option>
<option value="2">Regular sports</option>
<option value="3">No sports</option>
</select>
</div>
</div>
</div>
I’m I missing something? To recapitulate: I need the selected value (1,2 or 3) and NOT the text (“Regular sports” in the above example.
Well, there is an extra function to get the select changes.
Thanks to Raminson and thecodeparadox for pointing me to the right direction.