0

If I have the table:

 <table id="mastermind_table_one">
   <tr id="one">
     <td id='first'>First</td>
     <td id='second'>Second</td>
     <td id='third'>Third</td>
     <td id='forth'>Forth</td>
   </tr>
 </table>

How would I go about creating an array that contains each td?

I was hoping to get something like:

var array = ["First", "Second", "Third", "Forth"]
2
  • var array = $('#one td').map(function(){ return $.trim($(this).text()); }).get(). Commented Apr 7, 2014 at 20:05
  • Thank you all! I just tried this out and it worked. I appreciate it! Commented Apr 7, 2014 at 20:19

3 Answers 3

5

Like so:

var array = $("#one td").map(function() {
    return $(this).text()
}).get();
Sign up to request clarification or add additional context in comments.

1 Comment

@T.J.Crowder -- Yeah, I just thought about that -> changed it to target the row ID followed by children of that.
1
var newArray = [];
$('#one').children().each(function(){
  newArray.push($(this).html());
})

Comments

1

DEMO

var array = [];
$("#one td").text(function(i, txt) { array.push(txt); });

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.