0

Did build a function to return the index of element from a group of elements. But when I try to pass the value to another variable (myindex) I do get undefined.

Here the Javascript source code

$(document).ready(function(){
             $("[id*='_CG']").addClass("form-control input-sm text-right");
    $("[id*='_Weight']").addClass("form-control input-sm text-right");
    $("[id*='TXFuelWeight']").addClass("form-control input-sm text-right");
    $("[id*='TRPFuelWeight']").addClass("form-control input-sm text-right");

      myindex = GetLSElementIndex("[id*='W_OP_']", "W_OP_CAB1Items");
alert (myindex);

            });
 // Get the index element in a group of element
 function GetLSElementIndex(ElementGroup, Element) {
    $(ElementGroup).each(function (index) {
        var someText = $(this).attr('id');
        if (someText == Element) {
            alert(someText);
            alert(index);
            //GetLSElementIndex = index;
            //alert(GetLSElementIndex);
            return ;
            //return index;

        }


    });

}

Example

Jean-Marc Flamand
  • 939
  • 3
  • 10
  • 24
  • You are returning nothing i.e. `undefined` Try `return index` – Rayon Dec 10 '15 at 04:33
  • Use `return $(ElementGroup).filter('#' + Element).index();`. [**Demo**](http://jsfiddle.net/tusharj/a9tsz5uz/4/) – Tushar Dec 10 '15 at 04:37
  • 2
    You'll have to move the `return` out of the `.each()` iterator (and use another variable to provide the `index` value to it). The `return` keyword applies the value for the closest `function` that's around it. In this case, that's the `function (index) { ... }`. – Jonathan Lonowski Dec 10 '15 at 04:42

5 Answers5

0

You need to use like this:

var indx;
if (someText == Element) {
    alert(someText);
    alert($('#'+Element).index());//Don't do just alert(index)
    indx = $('#'+Element).index();//I need to find index of ? #W_OP_CAB1Items
     return indx;
  }
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0
function GetLSElementIndex(ElementGroup, Element) {
    var value = -1; //If no element is found, index is assumed to be -1
    $(ElementGroup).each(function (index) {
        var someText = $(this).attr('id');
        if (someText == Element) {
            alert(someText);
            value = index;
            alert(value);
            return false;//To break out of each() once the index is found.
        }
    });
    return value; //Return the value (from the called fn) once the processing is done

}
Rajasri.J
  • 148
  • 2
  • 17
0

Rework your GetLSElementIndex method like this by defining one global variable for that method and assign that index of each loop to that and return it and then bring the return statement out of the each loop.

DEMO: FIDDLE

// Get the index element in a group of element
    function GetLSElementIndex(ElementGroup, Element) {
        var setIndex;
        $(ElementGroup).each(function (index) {
            var someText = $(this).attr('id');
            setIndex = index;
            if (someText == Element) {
                alert(someText);
                alert(index);
            }
        });
        return setIndex;
    }

Note: Index of last item's of each loop only will be return by this method.

John R
  • 2,741
  • 2
  • 13
  • 32
0

You must break "Jquery each" and than return index back. point of confusion is "Jquery each" break by "return false" statement. You must store index value in some local variable(var toReturn .. in function below). here is the modified GetLSElementIndex function for you

 // Get the index element in a group of element
 function GetLSElementIndex(ElementGroup, Element) {
   var toReturn; 
    $(ElementGroup).each(function (index) {
        var someText = $(this).attr('id');

       if (someText == Element) {
            //alert(someText);
           // alert(index);
            toReturn= index;
            //GetLSElementIndex = index;
            //alert(GetLSElementIndex);
            return  false ;
            //return index;

        }

    });
     return toReturn;

} 

for more details please follow other question of Stack Overflow

Community
  • 1
  • 1
Lokinder Singh Chauhan
  • 2,326
  • 1
  • 20
  • 23
0

Update your function to store the index of desired element in a variable and then return the variable.

function GetLSElementIndex(ElementGroup, Element) {
   var elementIndex;/* store the index of #element here */  
   $(ElementGroup).each(function (index) {
       var someText = $(this).attr('id');
       if (someText == Element) {
           elementIndex = index;
       }
   });
   return elementIndex; /* will return index of #element if found otherwise will return undefined */
}

Have removed all the alerts as I assume those are for debugging purposes.

Gautham
  • 305
  • 4
  • 12