-1

Is there a more preferred way of doing this in terms of performance?

Example HTML:

<input type="text" data-attr="ABC XYZ" />
<div data-attr="XYZ"></div>

Example jQuery:

var myValue = 'XYZ';
$('[data-attr*="' + myValue + '"]').hide();

Is using classes better for this?

Pat Migliaccio
  • 1,031
  • 13
  • 24

2 Answers2

0

You could limit the selector only to input elements:

$('input[data-attr*="' + myValue + '"]').hide();

It reduces the surface area that the jQuery selector has to operate on

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

After doing some testing between data-attr show/hide vs. class show/hide, the difference is about 20% at 1,000,000 iterations. But, user experience doesn't necessarily seem affected by this in my application.

Pat Migliaccio
  • 1,031
  • 13
  • 24