Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set/get a bootstrap select items index

Status
Not open for further replies.

rogerte

Programmer
Nov 9, 2001
164
GB
Just started using PHP/MySql to convert old Access program. Using Bootstrap to speed things up. Generally it is going ok, except for using the Select input

In the data table dayofweek is stored as an integer (1=Monday etc).

I can get back that value ok, but am struggling with trying to show it as selected item in a Bootstrap Select input where the options are "Monday", "Tuesday"..."Sunday"

I assume it must be done with Javascript using the value from the database to set the index of the options, but have been struggling to see how this is done.

Similarly I need to convert the selected value back to the index to write back to the table.

I thought Google might have been my friend, but haven't found anything so far.

Can anyone give me a clue?

Thanks

 
Selects have a property called selectedIndex. You can use that to get or set the "selected" item for the select by making it equal to a specific number.

selectObj.selectedIndex=3; Sets the item with index 3 to be selected and shown in the dropdown as selected.
note indexes start at 0. So index 3 refers to the fourth item in the Select list.

Beyond that, when submitting the form to PHP, a Select will submit the value of the currently selected item. As long as the value property of the <option> element has the index value, you should not need to do anything else.

Code:
<select name="myselect">
[indent]<option [b]value="1"[/b]>Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>[/indent]
...
</select>



----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top