-2

So i have this HTML

<ul>
  <li onclick="clicked(this.placeinul)"></li>
  <li onclick="clicked(this.placeinul)"></li>
  <li onclick="clicked(this.placeinul)"></li>
</ul>
And this javascript

function clicked(placeInUl) {
  console.log(placeInUl)
}
So when i for example would click on the first li it would print 1 or if i clicked on the last li it would print 3. How would i do that?
MariusE
  • 37
  • 7

1 Answers1

1

EDIT: plain js version

function clicked(index) {
  alert(index+1);
}
li { cursor: pointer; }
<ul>
  <li onclick='clicked(Array.prototype.indexOf.call(this.parentElement.children, this));'>1</li>
  <li onclick='clicked(Array.prototype.indexOf.call(this.parentElement.children, this));'>2</li>
  <li onclick='clicked(Array.prototype.indexOf.call(this.parentElement.children, this));'>3</li>
</ul>
Celestine
  • 448
  • 7
  • 18