Often I come across situations while using combo box wherein I am required to handle multiple data.
During one such instance, I had to fetch a particular data list, its associated id as well as its corresponding category from different tables and then use it in the Form and when the user selects a value from the combo box, I have to save all the 3 information viz, the selected name, its id and its category.
Though it is pretty easy to store and get 2 values, I made use of an extra attribute to save the 3rd piece of information (category). I am sharing the code below.
Using jQuery I was able to retireve the category value which I had saved in `rel` tag
<select id="comboField" name="list_id" tabindex="1"> <option rel="Category-1" selected="selected" value="1">List-1</option> <option rel="Category-2" value="2">List-2</option> <option rel="Category-3" value="3">List-3</option> <option rel="Category-4" value="4">List-4</option> <option rel="Category-5" value="5">List-5</option> </select> |
jQuery Code:
$("#comboField").change(function(e){ $("#category").val($("option:selected", this).attr("rel")); $("#listname").val($("option:selected", this).text()); $("#listnumber").val($("option:selected", this).val()); }); |
Discussion