Solved

jQuery create an array

Posted on 2014-12-10
5
83 Views
Last Modified: 2014-12-16
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

0
Comment
Question by:Refael
  • 2
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40491725
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

0
 
LVL 51

Expert Comment

by:HainKurt
ID: 40491726
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
0
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 250 total points
ID: 40491734
Actually there's an extra line in my code.
Here's the clean version:
var idArray = [];
$('.table tr').each(function(idx, item) {
    var text = $(item).find('td:first').text();
  idArray.push(text);
});

Open in new window


But the main problem you have is that you're declaring the array inside the loop.
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 250 total points
ID: 40491760
more cleaner version :)

var idArray = [];
$('#myTable tr').each(function () {
    idArray.push($('td:first', $(this)).text());
});
alert(idArray );

Open in new window

0
 

Author Closing Comment

by:Refael
ID: 40503221
HainKurt, Alexandre Simões ... Thank you!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now