Solved

jQuery create an array

Posted on 2014-12-10
5
85 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:Huseyin KAHRAMAN
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:
Huseyin KAHRAMAN 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replace link with jQuery 4 24
how to use .length with "input" 2 31
JQuery Syntax... 4 50
Reactjs with .NET 3 70
DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
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 the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

685 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