Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find the position of the element in the whole html document

I don’t mean the position, x and y!
I want to find the position of the element based on its class. For example, in the code of the html document, we have 4 test classes. When one of them is clicked, I want to find the position that of elements among other friends who shared the same class.

<div>
    <div>
        <p class="test">one</p>
    </div>
    <p class="test">two</p>
    <span href="" class="test">three</span>
</div>
<div class="test">four</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use $.index:

const $els = $('.test');
$els.click(function() {
  const index = $els.index(this);
  console.log('The clicked element is the number', index);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading