4

How to get all value to array object using javascript ?

example html :

<tr>
  <td class="my_name">udin</td>
  <td class="my_name">juned</td>
  <td class="my_name">saepudin</td>
</tr>

how to get all value from class = "my_name" using javascript ?

my javascript :

<script>
  $(document).ready(function() {
    var array_of_name = $(".my_name");
    // how to get all value from my object array ?
    // i want get result like this : ["udin", "juned", "saepudin"] ?
  });
</script>

how to achieve it?

4
  • You don't have any array in your example. Commented Jun 24, 2014 at 8:48
  • What you want is to get the values of a collection of elements selected: api.jquery.com/each and api.jquery.com/html Commented Jun 24, 2014 at 8:48
  • I think he meant 'to' not 'from' Commented Jun 24, 2014 at 8:49
  • wrap text into DIV tag. var array_of_name = $(".my_name div"); You may want to play with Jquery JSON, and apply stringify to the object. Commented Jun 24, 2014 at 8:50

5 Answers 5

6

I think you're looking for this:

$( document ).ready(function() {
     var array_of_name = [];
     $(".my_name").each(function(){
         array_of_name.push($(this).text());
     }); 
     console.log(array_of_name); // will get result like this : ["udin", "juned", "saepudin"] ?

  });

OR

As per the comment you can use .map() too

var array_of_name= $(".my_name").map(function(){
        return $(this).text();
 }).get();

 console.log(names);

Docs

Demo

Demo with .map()

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

4 Comments

no, please use .map instead! Its entire purpose is to mutate the elements of an array to another (in this case, an array of elements to an array of their contents).
@Alnitak yup thought about map once.. :) but seriously .map() will be useful!
See how much nicer that code is now? ;-) The only complication is remembering to use .get to convert the jQuery-returned pseudo-array into a real array.
@Alnitak yup.. That looks good and also reduced line and object declaration. :) Thank you. :)
4

You may try this:

$(function(){
    var names = $(".my_name").map(function(){
        return $(this).text();
    }).get();
    console.log(names);
})

Comments

2

With modern browsers (MSIE >= 9) you can do this quite easily in plain JavaScript:

var names = [].map.call(document.getElementsByClassName('my_name'), function(el) {
    return el.textContent;
});

Demo

See also: Array.prototype.map

3 Comments

+1 for code that blatantly ignores MSIE's brokenness ;-)
@Alnitak Depends on which version of MSIE's brokenness ;-)
Granted I believe this would work perfectly on MSIE 9+
1

In Javascript, you can achieve it like this:

 var array_of_name = [];
 var elements_arr = document.getElementsByClassName("my_name");
 for (var i in elements_arr) {
     array_of_name.push(elements_arr[i].innerHTML);
 }
 console.log(array_of_name);

Comments

0

try this :

$(function(){
    var data = [];
    $(".my_name").text(function(key,value){
       data.push(value);
    });
 console.log(data);
})

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.