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

76
Views
Auto checkbox checked on page load based on value

I have dynamically generated checkbox from database and output looks like

<label class="check" for="encs_1"><input data-size="33400361270" type="checkbox" name="encs[]" id="encs_1" value="1"><b></b><i>checkbox1</i></label>
<label class="check" for="encs_2"><input data-size="118062750" type="checkbox" name="encs[]" id="encs_2" value="2"><b></b><i>checkbox2</i></label>

There is anyway with jquery to automatically checked field after page loaded based on data-size so the field with the lowest value will be checked?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could do this using JavaScript.

Try this code:

<script>
const encs = document.querySelectorAll("input[name='encs[]']");
var i = 0;   
for (i = 0; i < encs.length; i++) {
    if(encs[i].dataset.size=="118062750"){
       document.getElementById(encs[i].id).checked = true;
    }
}
</script>
about 4 years ago · Juan Pablo Isaza Report

0

You can get the input with the lowest data-size attribute by selecting all inputs, using jQuery.map to extract the data-size attribute, then using Math.min.

const min = Math.min(...$('input[data-size]').map(function(){ return $(this).data('size')}))

$('input[data-size="'+min+'"]').prop('checked', true);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="check" for="encs_1"><input data-size="33400361270" type="checkbox" name="encs[]" id="encs_1" value="1"><b></b><i>checkbox1</i></label>
<label class="check" for="encs_2"><input data-size="118062750" type="checkbox" name="encs[]" id="encs_2" value="2"><b></b><i>checkbox2</i></label>

about 4 years ago · Juan Pablo Isaza 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!