1

I need index in each function which has class = index,

My desired result is 2 and 4 and 5, it is come from td element base index.

How can I get tdbase indexing?

If you have opinion,please let me know.

Thanks

$(".index").each(function(){
let index=$(this).index()+1
console.log(index);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td>1</td>
    <td class="index">2</td>
    <td>3</td>
   </tr>
   <tr>
    <td class="index">4</td>
    <td class="index">5</td>
     <td>6</td>
   </tr>
 </table>

1
  • What results are you getting? Have you read the documentation for the .index() method to understand what it does? Commented Apr 25, 2020 at 3:04

5 Answers 5

2

$(this).index() is returning the index of the element relative to its parent element.

The callback function for .each() already receives the index of the element in the collection of matches, so if you want the index relative to the table you have to change the selector to a more general one and check if the element matches your condition.

$("td").each(function(idx) {
  if ($(this).is('.index')) {
    console.log(idx + 1)
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td>1</td>
    <td class="index">2</td>
    <td>3</td>
  </tr>
  <tr>
    <td class="index">4</td>
    <td class="index">5</td>
    <td>6</td>
  </tr>
</table>

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

Comments

1

You could do it in vanilla JavaScript like so:

let indices = document.querySelectorAll(".index");
console.log(Array.from(indices).map((i) => i.textContent));
<table>
  <tr>
    <td>1</td>
    <td class="index">2</td>
    <td>3</td>
  </tr>
  <tr>
    <td class="index">4</td>
    <td class="index">5</td>
    <td>6</td>
  </tr>
</table>

So, you grab all elements with class "index" with document.querySelectorAll(".index") then you convert them to an array so that you can map them and take their inner content with textContent

Comments

1

Try with: $(".index").each(function () { console.log($(this)[0].innerHTML); });

it wiil make the index the same as the value of the td.

Comments

1

$.each() function you can pass index and current html element $(".index").each(function(i, item)

And you can get td content by text() method, $(item).text() will get current td text.

$(".index").each(function(i, item){
let index=$(item).text();
console.log(index);
});

$(".index").each(function(i, item){
let index=$(item).text();
console.log(index);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td>1</td>
    <td class="index">2</td>
    <td>3</td>
   </tr>
   <tr>
    <td class="index">4</td>
    <td class="index">5</td>
     <td>6</td>
   </tr>
 </table>

Comments

1

If you want to use javascript and spread operator it more shorter.

[...document.querySelectorAll(".index")].map((item, index)=>{
  console.log(item.textContent);
});

[...document.querySelectorAll(".index")].map((item, index)=>{
  console.log(item.textContent);
});
<table>
  <tr>
    <td>1</td>
    <td class="index">2</td>
    <td>3</td>
  </tr>
  <tr>
    <td class="index">4</td>
    <td class="index">5</td>
    <td>6</td>
  </tr>
</table>

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.