jbeckom
jbeckom

Reputation: 105

CodeIgniter form_dropdown selected value

I have a form using CI's form_dropdown that pulls an array from DB....

$osValue = 'id = "osValue"';
echo form_dropdown('os', $os, '', $osValue);

which, when inspecting the element, is as follows in the user agent...

<select name="os" os>
<option value="1">Windows XP 32-bit</option>
<option value="2">Windows XP 64-bit</option>
<option value="3">Windows Vista 32-bit</option>
<option value="4">Windows Vista 64-bit</option>
<option value="5">Windows 7 32-bit</option>
<option value="6">Windows 7 64-bit</option>
<option value="7">Server 2003 32-bit</option>
<option value="8">Server 2003 64-bit</option>
<option value="9">Server 2008 32-bit</option>
<option value="10">Server 2008 64-bit</option>
<option value="11">Linux</option>
</select>

What I'm having trouble with is being able to pass ONLY the SELECTED value into a variable.

I get ALL options returned, when using

var os = $('#osValue').html();

or

var os = $('#osValue').text();

And, only the value (a numerical string passed in by the DB) is returned when using

var os = $('#osValue').val();

There has to be a small piece or two that I'm missing in order to have only the selected value returned. Any/all advice or help would greatly appreciated. Thank you in advance!

Upvotes: 0

Views: 10252

Answers (1)

stormdrain
stormdrain

Reputation: 7895

You have a couple of problems:

1) There is no ID on the select, yet you are querying for the id (you have an out-of-place "os" in there too; <select name="os" os> is not valid)

2) You aren't looking for which option has been selected

To fix, give the select an id:

<select name="os" id="os">
    <option value="1">Windows XP 32-bit</option>
    <option value="2">Windows XP 64-bit</option>
    <option value="3">Windows Vista 32-bit</option>
    <option value="4">Windows Vista 64-bit</option>
    <option value="5">Windows 7 32-bit</option>
    <option value="6">Windows 7 64-bit</option>
    <option value="7">Server 2003 32-bit</option>
    <option value="8">Server 2003 64-bit</option>
    <option value="9">Server 2008 32-bit</option>
    <option value="10">Server 2008 64-bit</option>
    <option value="11">Linux</option>
</select>

and query that from jQuery:

var osval = $('#os option:selected')

or, query by name:

var osval = $('[name="os"] option:selected')

http://jsfiddle.net/5Vmxd/

Upvotes: 1

Related Questions