Get custom attribute value from multiple select option using Jquery

Lets-c-codeigniter

I want to get custom attribute value from multiple select, here is my html,code

<select multiple name="tax[]" id="tax_classes" onChange="getSelectedOptions(this)">  
   <option value="2" data-percentage="9.00">CGST(9.00%)</option>  
   <option value="1" data-percentage="18.00">GST(18.00%)</option> 
   <option value="3" data-percentage="9.00">IGST(9.00%)</option> 
   <option value="4" data-percentage="12.00">Tax(12.00%)</option> 
</select>

From above tag, I wanted to get the data-percentage attribute value.

Here is the script what i tried so far

function getSelectedOptions(tax) 
{
  var options = [], option;
  var len = tax.options.length;
  for (var i = 0; i < len; i++) {
    option = tax.options[i]; 
    if (option.selected) { 
      options.push(option); 
      alert(option.value); 
    }
  }  
} 

I am getting output as 2,1,3,

But my expected output is 9.00, 18.00, 9.00 .

Constantin Groß

You say that you want to get the attribute value, but you are logging .value instead, which refers to the value attribute. You need to use getAttribute('data-percentage') instead.

function getSelectedOptions() {
  var options = [],
    option;
  var tax = document.getElementById('tax_classes');
  var len = tax.options.length;
  console.clear();
  for (var i = 0; i < len; i++) {
    option = tax.options[i];
    if (option.selected) {
      options.push(option);
      console.log(option.getAttribute('data-percentage'));
    }
  }
}
 
window.addEventListener('DOMContentLoaded', getSelectedOptions);
<select multiple name="tax[]" id="tax_classes" onChange="getSelectedOptions()">
  <option value="2" data-percentage="9.00">CGST(9.00%)</option>
  <option value="1" data-percentage="18.00" selected>GST(18.00%)</option>
  <option value="3" data-percentage="9.00">IGST(9.00%)</option>
  <option value="4" data-percentage="12.00" selected>Tax(12.00%)</option>
</select>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get value of a Custom Attribute using Javascript or Jquery

Using $_POST to get select option value from HTML

jQuery - getting custom attribute from selected option

veujs set custom attribute value in select option

Get value from select option by searching/filtering partial option text and output value as jQuery variable

custom value in custom attribute jquery select option

How to get attribute from selected option of select in jQuery?

Get Value of Disabled Option in Select Multiple Jquery

jquery to get value from multiple select box

Select option using keyboard based on value attribute

How to get data attribute from option in multiple select?

Get value of a custom attribute with jquery

Get custom attribute from select

Insert a "Select Value" option in dropdown list from array using jQuery

Get index or value of clicked option from multiple select

Get key value for selected option in select using jquery

JavaScript/jQuery - Get option attribute from select menu by text

How to get the value of custom HTML attribute using JQuery?

Get Select Tag Value not Option Value jQuery

get select option value using onchange function in jQuery from multiple dynamic record?

get select option value using $(document).ready function in jQuery from multiple dynamic record?

Get Attribute Value From Specific Select Option

To get Custom attribute value present in <ul> tag using jquery

select onchange get option name attribute value

jQuery get multiple data from custom attribute

Get option value from custom select?

jQuery Select2 - get style attribute from original option's select

How can i get multiple value using dropdown select option data attribute?

How to get only last selected option value from multiple select using jquery