1

I have some tables that have data and can using it on <td>. So more like it I have something like this (show on images below)

My Element

enter image description here

I want to get that all positions Name and put it into an array so I can make of use that array I tried to use this code and got undefined

script.js

/** Checking if There positions name */
function checkPositions(){
   let positions = document.getElementsByClassName('check-positions').innerHTML;
   let array = [];
   array.push(positions);
   console.log(array);
}

Then how can I get that value??

4 Answers 4

3

The problem that you have is that document.getElementsByClassName('check-positions') returns a HTMLCollection which does not have an innerHTML property.

What you need to do is convert the HTMLCollection into an array, and then read the innerHTML property for each of the items in the array. See the following example:

const elements = document.getElementsByClassName('check-positions');
const positions = Array.from(elements).map(element => element.innerHTML);

console.log(positions);
<div class="check-positions">1</div>
<div class="check-positions">2</div>
<div class="check-positions">3</div>

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

Comments

1

Use like this

   let positions = document.getElementsByClassName('check-positions')[0].innerHTML;

It's showing none because u r fatching whole array and pushing it without using indexes

Code

function checkPositions(){
       all_ele = document.getElementsByClassName('check-positions')
       length = all_ele.length
       let array = [];
       for( let i=0;i<length;i++)
       {
          let positions = document.getElementsByClassName('check-positions')[i].innerHTML;
          array.push(positions);
       }   
       console.log(array);

Comments

0

you can use jquery code to do this.

var arr = [];
$("#tablePlacement tr").each(function() {
var name =  $(this).children('td.check-positions').text();
arr.push(name);
});

Comments

-1

You should use

let positions = document.getElementsByClassName('check-positions').innerText;

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.