Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery match data attribute to values in an array

I have a list kind of like below:

<p class='info' data-info='1'>Info 1 <span class='status'></span></p>
<p class='info' data-info='2'>Info 2 <span class='status'></span></p>
<p class='info' data-info='3'>Info 3 <span class='status'></span></p>......etc

What I need to do is run through all the .info elements and if the data-info number is included in an myArray change the span text. How, in jQuery do I match a value from inside an array ?

like image 933
14 revs, 11 users 47% Avatar asked Aug 02 '26 23:08

14 revs, 11 users 47%


1 Answers

$('.info').each(function() {
  $this = $(this);
  if ($.inArray($this.data('info'), myArray) !== -1) {
    $this.children('.status').text('New text value');
  }
});

Here's a fiddle

like image 95
billyonecan Avatar answered Aug 05 '26 12:08

billyonecan