overflowstack9 overflowstack9 - 4 months ago 11
jQuery Question

How can i give numbering for option values

The below is my code but i want to give number for every value like this

<option value="1" />vijay


But
apple+
should be there because i want to assign some values using jquery thats i want to add + value as we can modify
<option value="1" />vijay


but due to my template design i want to give like this
<option value="1" value=vijay+/>vijay
i dont think it is correct what is the correct format.

<select id="cmbColumn">
<option value="" />Columns
<option value="apple+" />apple
<option value="berry+" />berry
</select>

<select id="cmbSidebar" name="cmbSidebar">
<option value="" />Sidebars
<option value="grapes+" />grapes
<option value="mango+" />mango
</select>


my main aim is to achieve for example like for all values apple+ and apple and also giving number to the values.

This is my other code but i want to give numbering to values like this but like i above said i also need grapes+ values with them

<select id="cmbsidebar">
<option value="1" />vijay
<option value="2"/>1004045

</select>

<select id="combo-014">
<option value="1" />1003045
<option value="2" />1004045

</select>

Answer

You can use data attributes together with the value attribute like this:

document.getElementById("options").onchange = function(e) {
  // Get the selected option element
  var target = e.explicitOriginalTarget;
  
  // Get its value
  console.info(target.getAttribute("value"));
  
  // Get its data-value
  console.info(target.getAttribute("data-value"));
}
<select id="options">
  <option value="">Sidebars</option>
  <option value="1" data-value="grapes+">grapes</option>
  <option value="2" data-value="mango+">mango</option>
</select>

Comments