13

Could not find a clear and recent explanation of how to achieve this. Does jQuery have a straightforward method for taking the entire third column from a HTML table with id="table1" and populating an array with one cell value per array element. I am relatively new to jQuery and have not explored its features fully. Some of jQuery's shortcuts have amazed me so thought it might be wiser to ask here than to go on mashing code together and seeing no results.

1

2 Answers 2

12

To build a array out of all elements from 3rd column you can using the following code

var colArray = $('#table1 td:nth-child(3)').map(function(){
       return $(this).text();
   }).get()​;

here What I am doing is selecting all cells in 3rd column by nth-child selector. Then using $.map function to loop through to and use their value to build a array.

Working Fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this. Is there any easy way to display the values in an alert or even document.write? Thank you.
Click on the link I have provided at the bottom of my answer and you will get an alert.
Or even better check this link, jsfiddle.net/joycse06/tmfxr/1 It shows an alert and adds the values into an div too. :)
Thank you Joy. This achieves what I needed.
4

You can try this :

var myArray = new Array();
$(document).ready(function() { 
    $("#table1 tr td:nth-child(3)").each(function(i){
       myArray.push($(this).text());
    });
});

Here is an example http://jsfiddle.net/nU6bg/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.