0

EDIT jQuery.inArray kept failing to do string != int comparisons, and this was resolved with the help of ShankarSongoli. Originally it was thought to be a variable scope issue, so please remember to look at all aspects and not just one in any given problem.

   omit_all_user_contacts = new Array();
    add_contacts = new Array();

    function is_added_contact(id) {
        if ( jQuery.inArray(id, add_contacts) == -1 )
            return false;
        return true;
    }

    function selectcontacts(userid){
      document.getElementById("ic"+userid).checked = true;
      omit_all_user_contacts.push(userid);
      var buf = '<table class="contactlist" width="100%">';
      buf = buf + "<tr class='contactlistheader'><td></td><td>Contact Name</td><td>Company</td><td>Email</td><td>Profession</td></tr>";
      var tmp;
      jQuery.getJSON('/user/get_user_contacts/'+userid, function(data) {

          jQuery.each(data.contacts, function(key, value) {

            buf = buf + "<tr><td><input type='checkbox' name='manualcontact[]' onclick='manualcontact(" + value.id + ",this.checked)' ";
                alert(is_added_contact(value.id)); 

            if ( jQuery.inArray(value.id, add_contacts) > -1 ) {

                buf = buf + " checked ";
            }
            buf = buf + "/></td>";
            buf = buf + "<td>" + value.firstname + " " + value.lastname + "</td>";
            buf = buf + "<td>" + value.company + "</td>";
            buf = buf + "<td>" + value.email + "</td>";
            buf = buf + "<td>" + value.profession + "</td></tr>";
          });
          buf = buf + '</table>';
          iBox.show(buf);
      }); 
    }

    function manualcontact(id,val) {
        if ( val == true ) {
            add_contacts.push(id);
        } else {
            add_contacts.splice( jQuery.inArray(val, add_contacts), 1 );
        }   
    }
8
  • Your code looks fine. Try it again with using var for both the variables. What error do you get? Commented Jan 26, 2012 at 18:32
  • Hello, thanks for the response. When manualcontact is called, the value is added to the add_contacts array. However, when selectcontacts is called, the add_contacts array disappears once it starts this loop: jQuery.each(data.contacts, function(key, value), but in the previous line, this variable IS visible, I just need it to be accessible within the each statement as well. Commented Jan 26, 2012 at 18:36
  • Since it is a global variable it will he accessible inside each loop also. Commented Jan 26, 2012 at 18:39
  • I wish that were true in this case, but is not... I also just tried a workaround I really thought would work,by setting tmpContacts = add_contacts; in the line before the each loop. However, tmpContacts is also, inaccessible from within the each statement. :( Commented Jan 26, 2012 at 18:40
  • What do you mean by inaccessible? Do you get error when code execution reaches that point? Commented Jan 26, 2012 at 18:46

2 Answers 2

2

You should change the title of the question first and also since the issue is resolved through our comments accept this answer and close it.

Solution:

The array values were of string type and comparison was done with integers, so it was failing.

var tmpContacts = ["2"];
jQuery.inArray(2, tmpContacts) was always returning `-1`

Change it to jQuery.inArray("2", tmpContacts)

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

Comments

0

It is important that the type of the variable is the same. I needed to compare integers, but since my original text contained numbers split by commas then the items in my array were being seen as strings.

Here was a simple function I wrote to check for integers only. It works the same as jquery.inArray but it only checks a specific data type.

function in_array(val,arr) {
    cnt=0;
    index=-1;
    $(arr).each(function () {
        if(parseInt(this) == parseInt(val)) { index=cnt; }
        cnt++;
    });
    return index;
}

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.