Becky Becky - 1 year ago 108
Javascript Question

Get text from an array

I got an array wich has button elements in a certain order.

var arr = [ $('.buttons')[0], $('.buttons')[2], $('.buttons')[1], $('.buttons')[4] ];

How do I get the text of each button? Below is what I tried.

$.each(arr, function (index, value) {
var butText = value.text();
//do something...

Answer Source

You need to use jQuery object $(this) to get button text.

var arr = [$('.buttons')[0], $('.buttons')[2], $('.buttons')[1], $('.buttons')[4]];

$.each(arr, function () {
    var butText = $(this).text();
<script src=""></script>
<button class="buttons">button 0</button>
<button class="buttons">button 1</button>
<button class="buttons">button 2</button>
<button class="buttons">button 3</button>
<button class="buttons">button 4</button>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download