[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

jQuery create an array

Posted on 2014-12-10
5
Medium Priority
?
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 60

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 1000 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 60

Accepted Solution

by:
HainKurt earned 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…
Suggested Courses

650 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