0

im using bootstrap, how can i get the attribute of ie., gl_code_id

<select style="height: 400px;" multiple="" class="form-control" id="all_codes">
           <option gl_code_id="9">2345 </option>
           <option gl_code_id="8">12345 </option>
           <option gl_code_id="7">1234 </option>
           <option gl_code_id="6">90 </option>
           <option gl_code_id="5">234 </option>
           <option gl_code_id="4">1 </option>
</select>

tried this,

$(document).on("change", "#all_codes", function(){
    var id = $("#all_codes").val(); //this gives only the value
    var ids = $(this).attr('gl_code_id'); // this gives undefined
    //how can i get the selected options gl_code_id??

});

3 Answers 3

1

You can use:

$("#all_codes option:selected").attr('gl_code_id');

Since you're using multiple attribute for your select, if you want to retrieve all the gl_code_id value from multiple selected option, you can use .map():

$(document).on("change", "#all_codes", function(){
    var id = $("#all_codes").val(); //this gives only the value
    var ids = $(this).find('option:selected').map(function() {
        return $(this).attr('gl_code_id');    
    }).get();
});

Fiddle Demo

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

Comments

0

Try this

 var ids = $(this).find('option:selected').attr('gl_code_id'); // return attr value

 $(this).find('option:selected').text(); // return selected option text

Comments

0

Try

you need to .find('option:selected')

var ids = $(this).find('option:selected').attr('gl_code_id');

.find() and :selected Selector

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.