Javascript JQuery find element by data attribute



  • HTML
<div class="form-group ">
    <label for="Model[name]">Name</label>
    <input data-propertyX="example" id="Model_name" name="Model[name]" class="form-control" type="text" placeholder="Name" value="">
  • Javascript search by data-property
$( document ).ready(function() {
  var searchBY = 'example';
  // select by class or another html label and then filter by data attribute name
  var fieldForm = $('.form-control').filter('[data-propertyX="' + searchBY + '"]');
  // now we can do whatever with the input field 
  fieldForm.val('Field selected by data-propertyX');