Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Selecting an option based on the value of its data attribute

I want to choose (aka jQuery preselect) and set the value based on data variable. The list has 3 items having same values but differing data-d.

Suppose I have variable selectvar="d" then it should change to 2 item in the list.

<select id="select2">
    <option data-id="mo" value="1" style="display: none;">1</option>
    <option data-id="d" value="1" style="">one</option>
    <option data-id="h" value="1" style="display: none;">o-ne</option>
    <option data-id="mo" value="2" style="display: none;">2</option>
    <option data-id="d" value="2" style="">two</option>
    <option data-id="h" value="2" style="display: none;">t-wo</option>
    <option data-id="mo" value="3" style="display: none;">3</option>
    <option data-id="d" value="3" style="">three</option>
    <option data-id="h" value="3" style="display: none;">t-hree</option>
</select>

val = 2
selectvar="d"
$("#select2 option[data-id='d'][value='2']").prop("selected", true);
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use below code and give id to your select.

$("#select2 option[data-id='" + selectvar + "']").prop("selected", true);
over 4 years ago · Santiago Trujillo Report

0

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>

over 4 years ago · Santiago Trujillo Report

0

$("button").click(function() {
  var selectVal = $("input").val();
  
  $('[data-id="' + selectVal + '"]').prop("selected", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option data-id="mo" value="1" style="display: none;">1</option>
  <option data-id="d" value="1" style="">one</option>
  <option data-id="h" value="1">o-ne</option>
</select>
Data-id:<input/> <button>Change</button>

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!