JavaScript Selecting an option based on the value of its data attribute

Set the selected variable - then iterate over teh select list options and compare the data attribute - it the same - set selected property.

var selectvar = "d";


 $("#select2 option").each(function(){
     var id= $(this).attr('data-id');
    if(id === selectvar){ 
      $(this).prop('selected',true);
    }
 })
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select2">
  <option data-id="mo" value="1">1</option>
  <option data-id="d" value="2" >one</option>
  <option data-id="h" value="3">o-ne</option>
</select>
Answer:1

I'm doing a personal proyect where the user can configurate their car online. The basis is simple: 3 buttons (Change Color - Change View - Lights On/Off) and the following sprite image: As you can ...

I'm doing a personal proyect where the user can configurate their car online. The basis is simple: 3 buttons (Change Color - Change View - Lights On/Off) and the following sprite image: As you can ...

a simple question. I want to use CSS Zoom level to resize my Site oncklick. I want to catch the css zoom from "html" element. CSS html{zoom:1} JavaScript function zoomIn(){ altzoom = document....

a simple question. I want to use CSS Zoom level to resize my Site oncklick. I want to catch the css zoom from "html" element. CSS html{zoom:1} JavaScript function zoomIn(){ altzoom = document....

  1. javascript zoom level of browser
  2. javascript zoom level detect
  3. javascript get zoom level
  4. javascript set zoom level
  5. javascript change zoom level
  6. javascript window zoom level
  7. javascript get zoom level of browser
  8. javascript check zoom level
  9. javascript set zoom level of browser
  10. javascript chrome zoom level
  11. javascript reset zoom level
  12. javascript detect zoom level browser
  13. javascript determine zoom level
  14. maps javascript zoom level
  15. arcgis javascript zoom levels
  16. javascript find zoom level
  17. javascript control zoom level
  18. javascript read zoom level
  19. javascript ie zoom level
  20. esri javascript zoom levels

I have done this to create number of columns based on the data my server returns back. I want to check if the data in the array matches the column name, if it does then return yes else return no. ...

I have done this to create number of columns based on the data my server returns back. I want to check if the data in the array matches the column name, if it does then return yes else return no. ...

  1. datatable rows add array

I am struggling with an excercise question and maybe you could help me. I have to set up a camera and cube with THREE.js. The cube should keep its initial ratio and the camera should look at the ...

I am struggling with an excercise question and maybe you could help me. I have to set up a camera and cube with THREE.js. The cube should keep its initial ratio and the camera should look at the ...