list - jquery find index of element with selector -


I'm trying to use jQuery's .index () function and I can understand how it works Does. There are no results which I expect from them. This is a little example why I do not get it:

HTML:

  & lt; div id = "gallery" & gt; & Lt; Div class = "gallery-item" & gt; Item 1 & lt; / Div & gt; & Lt; Div class = "gallery-item active" & gt; Item 2 & lt; / Div & gt; & Lt; Div class = "clear view-LG" & gt; & Lt; / Div & gt; & Lt; Div class = "gallery-item" & gt; Item 3 & lt; / Div & gt; & Lt; Div class = "gallery-item" & gt; Item 4 & lt; / Div & gt; & Lt; / Div & gt; & Lt; Div id = "button" & gt; & Lt; Button ID = "Next" & gt; Next & lt; / Button & gt; & Lt; / Div & gt; & Lt; Span id = "console" & gt; & Lt; / Span & gt;   

js:

  jquery (document) .ready (function ($) {$ ("# button> #next"). (Function () {var items = $ ("gallery-item"); var index = items.find ("active.") Index (); $ ("# console"). Attachments ("index:" + index + "Index (with index:" + indexWithSelector + "
"
"var indexWithSelector = items.index (" active "); $ (" # console " );) Var prevAll = items.prevAll ("Gallery-Items"). Size (); $ ("# console"). Attachments ("prevailing:" + prevAll + "


" ; $ Each (item, function (index, value) {$ ("# console"). Append ("
"+ val.className";});});});

I hope all the results will be the same: 1, because the active element is zero - The second is based on the list. But I get this:

  Index: 1 Selector with pointer: -1 prevall: 3 gallery-items gallery items active gallery-items gallery-items < / Code>  

Why? Is something wrong with me?

Update:

I tried to search the active element directly and find it For the index of The sum and it worked. I tried to do this:

  var active = $ (". Gallery-item.active"); Var index = items.index (active); // index == 1    

I have checked your code in jsfiddle. I found out that you are receiving -1 because the element element present in that element does not exist:

  var item = $ ("gallery-item"); Var index = items.find ("active"). Index ();   

Items here. The package will return nothing but empty jacuzzi objects. You are trying to find an active class in the child item of the gallery item. But the active class is at the same level as the other gallery -im. Then the code should be:

  var index = items.filter ("active"). Index ();   

That's why you were getting this -1. There was no such element, index was returning as 1.

  index: -1   

Let's talk about this now:

  Selector with indicator: -1   

The reason behind this -1 is the following code:

  var indexWithSelector = items.index ("active");   

Here you are getting $ ("gallery-item") in the Active Class store. Which is not making any sense. As such. Active gallery is part of the item.

The search must be like this:

  var indexWithSelector = items.filter ("active"). Index ".gallery-item");   

Now lets check the following code:

  var prevAll = items.prevAll ("Gallery-Items"). Shape ();   

Here you are trying to get the size of all the previous objects. The size of the item is 4 dollars i.e. ("gallery-item") and its size is 4-1 = 3 which is correct.

If you are trying to get the previous gallery item of the active category, then the code should be:

  var prevAll = items.filter (".active". ) prevAll ("Gallery item") size () ..   

Then the correct output will look like this:

  Index: 1 indicator with selector: 1 prevail: 1   

Last but not least here is JSfield. I commented along with your code so that you can comment and check the effect:



Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -