0

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!

2
  • First, <select name="os" os> seems defective, make sure form_dropdown() works properly. Second, what's wrong with .val()? Commented Jan 24, 2014 at 21:15
  • I'm unable to assign an ID, as this information is populated by CodeIgniter without an option for ID. Commented Jan 24, 2014 at 21:43

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your help on this... just adding the "option:selected" to the jquery variable made this work flawlessly for me!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.