Coder Perfect

jQuery is used to add more data to choose options.


I’m hoping this is a straightforward inquiry.

I have the standard select> box, which looks like this.

<select id="select">
    <option value="1">this</option>
    <option value="2">that</option>
    <option value="3">other</option>

Using $(“#select”), I can access the selected value. val()) and the display value of the selected item (using $(“#select:selected”).text ().

But how can I save anything like an extra value in the option> tag? I’d like to be able to do something like option value=”3.1″ value2=”3.2″>other/option> to get the value2 attribute’s value (which would be 3.2 in the example).

Asked by jim smith

Solution #1

HTML Markup

<select id="select">
  <option value="1" data-foo="dogs">this</option>
  <option value="2" data-foo="cats">that</option>
  <option value="3" data-foo="gerbils">other</option>


// JavaScript using jQuery
       var selected = $(this).find('option:selected');
       var extra ='foo'); 

// Plain old JavaScript
var sel = document.getElementById('select');
var selected = sel.options[sel.selectedIndex];
var extra = selected.getAttribute('data-foo');

Here’s a jQuery-based functioning example: This is a working example using basic JavaScript.

You can add extra data to components in a syntactically-valid manner that is also conveniently accessible from jQuery by using data attributes from HTML5.

Answered by Phrogz

Solution #2

It appears to me that you wish to create a new attribute. Do you wish to

<option value="2" value2="somethingElse">...

You have the ability to complete this task.

$(your selector).attr('value2', 'the value');

Then you can use it to get it back.

$(your selector).attr('value2')

It’s not going to be legal code, but I suppose it’ll suffice.

Answered by mikesir87

Solution #3

I came up with two examples based on what I believe your question is:

For saving more values, take a look at this. It uses data attributes to store the other value:

Answered by zsalzbank

Solution #4


<Select id="SDistrict" class="form-control">
    <option value="1" data-color="yellow" > Mango </option>

JS when initialized

        create: false,
        sortField: 'text',
        onInitialize: function() {
            var s = this;
            this.revertSettings.$children.each(function() {
                $.extend(s.options[this.value], $(this).data());
        onChange: function(value) {
            var option = this.options[value];
            alert(option.text + ' color is ' + option.color);

With option, you can access the data attribute of the option tag. [data-attribute] p/9cqoaeyt/3/ JS Fiddle: p/9cqoaeyt/3/

Answered by Shashank Pujari

Solution #5

HTML/JSP Markup:

data-raison="${compte.libelleSociale}"   data-rib="${compte.numeroCompte}"                              <c:out value="${compte.libelleCompte} *MAD*"/>


var $this = $(this);
var $selectedOption = $this.find('option:selected');
var libelle = $'libelle');

To have a libelle.val() or libelle.text element ()

Answered by Fadid

Post is based on