Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

jQuery create an array

Hello Experts,

I am trying to create an array : looping on all the rows in a table and garbing each first column text.
Basically this should give an array of numbers e.g. 01, 09, 30 and etc... The below code makes my browser freeze :-))

$('.table tr').each(function() {
  var idArray = [];
  var firstTd = $('td:first', $(this));
  var firstTdText = firstTd.text();
  idArray.push(firstTdText);
});

Open in new window

Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

var idArray = [];
$('.table tr').each(function(idx, item) {
    var firstTd = $(item).find('td:first').text();
  var firstTdText = firstTd;
  idArray.push(firstTdText);
});

Open in new window

This works fine:

<script type='text/javascript'>
$(window).load(function(){
var idArray = [];
$('#myTable tr').each(function () {
    var firstTd = $('td:first', $(this));
    var firstTdText = firstTd.text();
    idArray.push(firstTdText);
});
alert(idArray);
});
</script>

  <table id=myTable>
    <TR>
        <TD>01</TD>
        <TD>Test 01</TD>
    </TR>
    <TR>
        <TD>14</TD>
        <TD>Test 02</TD>
    </TR>
    <TR>
        <TD>23</TD>
        <TD>Test 03</TD>
    </TR>
</table>

Open in new window


test it here
SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Refael

ASKER

HainKurt, Alexandre Simões ... Thank you!!!