Hello I would like to fill gradually span elements with values of array. Can you help me please?
<span class="gt"></span>
<span class="gt"></span>
<span class="gt"></span>
var array=["apple","banana","cucumber"];
$("span.gt").each(function(){
$(this).text(array[?]);
});
Output should look like this:
<span class="gt">apple</span>
<span class="gt">banana</span>
<span class="gt">cucumber</span>
You get the index
value in the function for each
loop. And since the number of span
elements is equal to the number of items in the array
variable you can use that index
value to set the array values in the span
elements:
var array=["apple","banana","cucumber"]; $("span.gt").each(function(index){ $(this).text(array[index]); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="gt"></span> <span class="gt"></span> <span class="gt"></span>
var array = ["apple", "banana", "cucumber"]; for (var i = 0; i < array.length; i++) { $('span').eq(i).text(array[i]); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="gt"></span> <span class="gt"></span> <span class="gt"></span>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.