Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - get text file, read it and then add results as class to multiples div

I have some data provided from a file.txt wich are display like this :

<div class="nbvote">465</div>
<div class="nbvote">12</div>
<div class="nbvote">1862</div>
[...]
<div class="nbvote">3</div>

And i have in the same page multiple div (90) like this :

<div class="grid_4">
<div class="grid_4">
<div class="grid_4">
[...]
<div class="grid_4">

Now i want to add each data (465,12,1862,...,3) as class to each div class="grid_4" like this :

<div class="grid_4 465">
<div class="grid_4 12">
<div class="grid_4 1862">
[...]
<div class="grid_4 3">

How can i do that ? I was thinking using lenght, but i wasn't be able to do this.

By the way, the data are the number of vote and i would like to add them as class to each div grid_4 and then sort ascending.

Sorry for my english, not my first langage.

Ty

like image 621
user1092395 Avatar asked Nov 23 '25 06:11

user1092395


1 Answers

try this:

$('.nbvote').each(function(i, v){
   var cls = $(this).text();
   $('.grid_4:eq('+i+')').addClass(cls)
})

DEMO

note: class names Must begin with a letter A-Z or a-z

like image 163
undefined Avatar answered Nov 24 '25 23:11

undefined