0

I'm having problem with Jquery inArray.

//Get selected devices
selectedDevices = $('.selectDevices').val();
console.log(selectedDevices);

//If nothing selected, do nothing
if(selectedDevices == null || selectedDevices == ""){}

    //Else remove devices not selected
    else{ 
        //Loop thru table rows with class "enhet"
        $.each( $('td.enhet'), function() {

        thisDevice = $('.enhet').text();

        var found = $.inArray(thisDevice, selectedDevices);

        console.log(found);

        if (found > -1) {
            console.log(thisDevice);
        }
        else {
            console.log('nope');
        }


    })
}

Console.log(selectedDevices) give:
["GulAvformning", "RosaAvformning"]

Console.log(found) give: (everytime!!)
-1

Console.log(thisDevice) give:
``

Console.log('nope') give:
nope

Even if thisdeviceexists in selectedDevices i get the else case.
What am i missing?

selectedDevices is an array. I checked it by: console.log(selectedDevices[1]); And it gave me one of the devices.

2
  • 3
    It doesn't work because both are strings. Commented Feb 16, 2016 at 12:16
  • thisDevice = $('.enhet').text(); what is the value of thisDevice Commented Feb 16, 2016 at 12:18

4 Answers 4

2

Use the each() method to the elements and then use $(this) to identify the current element:

selectedDevices = $('.selectDevices').val();
console.log(selectedDevices);

//If nothing selected, do nothing
if(selectedDevices == null || selectedDevices == ""){}

//Else remove devices not selected
else{ 
  //Loop thru table rows with class "enhet"
  $('td.enhet').each(  function() {

    thisDevice = $(this).text(); // use $(this) here

    var found = $.inArray(thisDevice, selectedDevices);

    console.log(found);

    if (found > -1) {
      console.log(thisDevice);
    }
    else {
      console.log('nope');
    }


  })
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes of corse, that was my problem! Thank you!
Well, my pleasure, check this answer as correct when you can make it. Thank you!
1

Use:

thisDevice = $(this).text();

Instead of:

thisDevice = $('.enhet').text();

Comments

0

You are iterating but not selecting the iterator. Change your lines to this: ...

$.each( $('td.enhet'), function(i) {

        thisDevice = $('td.enhet').eq(i).text();

...

4 Comments

better to use the element itself returned within the each callback...otherwise you are searching the DOM each iteration to look for that same element. DOM searches are expensive
I agree. I was not sure with "this".
Use $('td.enhet').each(... and this is current element
I know. but I wasn't sure of it, never tried, so I didnt use "this". I stand corrected
0

If console.log(selectedDevices) gives ["GulAvformning", "RosaAvformning"], and console.log(thisDevice) gives "", then $.inArray(thisDevice, selectedDevices) will return -1, since there's no "" in ["GulAvformning", "RosaAvformning"]. See Marcos Pérez Gude's solution for more info about how to properly get the correct value for thisDevice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.