Solved

jQuery create an array

Posted on 2014-12-10
5
80 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now