JavaScript jQuery index() not jquery indexof,jquery indexof array,jquery indexof is not a

When i click on a element with a class named sections, i should get an alert showing what is it's class index, but it's showing a wrong number.

For example, if i click the first div element with a class named sections, javascript alert should say 1, not 3.

Is there a way to target class indexes with click() using jQuery?

<!doctype html>

<html>

<head>
<meta charset="utf-8">
<title>this is the title</title>
<style>
.sections {
    width:200px;
    padding:30px;
    background:blue;
    margin:20px;
}
</style>
</head>
<body>

    <div></div>
    <div></div>
    <div></div>

    <div class="sections"></div>
    <div class="sections"></div>
    <div class="sections"></div>

    <script src="jquery-1.8.2.min.js"></script>
    <script src="script/script2.js"></script>
    <script>
    $('.sections').click(function() {
        x = $(this).index();
        alert(x);
    });
    </script>

</body>
</html>
Answer:1

You need to pass in the selector against which you want to index of ( if you want to get the index based on the specific class )

x = $('.sections').index(this);

$('.sections').click(function() {
  x = $('.sections').index(this);
  alert(x);
});
.sections {
  width: 200px;
  padding: 30px;
  background: blue;
  margin: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>


<div class="sections"></div>
<div class="sections"></div>
<div class="sections"></div>
Answer:2

You are correct index(), is used to find index of an element with respect to its siblings but correct way to use it in your code is.

$('.sections').click(function() {
   var x = $('.sections').index(this);
   alert(x);
});

Also, please note that it will start from 0 instead of 1.

Answer:3

https://api.jquery.com/index/

So, as you noticed, it's alerting it's index for itself among it's siblings in the DOM. (starting at 0)

If you want to get it's index among similar objects, you can use .index slightly differently.

$('.sections').click(function(){
    x = $('.sections').index($(this));
       alert(x);
});

This is still 0 based, so you can now expect that it will show 0 for the first item, then 1 and 2.

Answer:4

How can I get the class name of the parent div from which a html link calls a javascript function? In the following code example: <div class="parent-class"> <a href="javascript:parent(...

How can I get the class name of the parent div from which a html link calls a javascript function? In the following code example: <div class="parent-class"> <a href="javascript:parent(...

  1. get class name parent jquery
  2. css class name parent
  3. jquery class name parent
  4. python class parent name
  5. ruby class parent name
  6. python get class parent name
  7. parent class name javascript
  8. parent class name
  9. php get class parent name
  10. parent class name php
  11. get class name of parent
  12. get class name of parent div jquery
  13. parent class name c++

So I have a service exposed to multiple angular controllers. This service checks if our access token is valid, and if it isn't, makes a request to the server to get a new access token (by sending a ...

So I have a service exposed to multiple angular controllers. This service checks if our access token is valid, and if it isn't, makes a request to the server to get a new access token (by sending a ...

I have a listView of items and these items are to be grouped together based on a class and shown with boxes. Suppose I have 5 items with: <div class="1"></div> <div class="1"></...

I have a listView of items and these items are to be grouped together based on a class and shown with boxes. Suppose I have 5 items with: <div class="1"></div> <div class="1"></...

Trying to figure out the cleanest way to do the following: I'm filtering some results twice, and I'm using Lodash filter to do so. Currently, my code looks like: resultsOne = _.filter(results, ...

Trying to figure out the cleanest way to do the following: I'm filtering some results twice, and I'm using Lodash filter to do so. Currently, my code looks like: resultsOne = _.filter(results, ...

  1. lodash filter with multiple conditions
  2. lodash filter with index
  3. lodash filter with or condition
  4. lodash filter with multiple values
  5. lodash filter with array
  6. lodash filter with regex
  7. lodash filter with keys
  8. lodash filter with map
  9. lodash filter with includes
  10. lodash filter with function
  11. lodash filter with or
  12. lodash filter with limit
  13. lodash filter with object
  14. lodash filter with sort
  15. lodash filter with chain
  16. lodash filter with predicate
  17. lodash filter by value
  18. lodash filter by property
  19. lodash filter by date
  20. lodash filter by multiple properties