Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Can I use jQuery event to access object from an associative array?

    Hi

    I have a "delete" script in jQuery which doesn't seem to work. My suspect is that the class Im trying to access comes from an associative array.

    My goal is when the image is clicked, an alert box appears with the itemID.

    The array is working fine, its just the delete script. There are no errors being displayed by Javascript Console from Chrome. The table just flickers when I press the image on my current script.

    Here are my scripts...

    Array
    Code:
    for (var i in tempList){
    var itemID = i;
    var split = tempList[i].split('_');
    var kilo = split['0'];
    var numBags = split['1'];
    list+='<tr><td>'+itemID+'</td><td>'+kilo+'</td><td>'+numBags+'</td><td><img src = "../../../resources/images/icons/cross.png" class="delete" id="'+itemID+'" /></td></tr>';
    }
    $('#returnlist').html(list);
    });
    Delete Script
    Code:
    $('.delete').click(function(){
    var deleteid = this.id;
    alert(deleteid);
    });
    Last edited by JustBob; 09-27-2011 at 03:33 PM. Reason: additional info

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Would need to see more of your code in order to know if you assign the click event for delete after the elements are created etc...

    For now all I can do is asking you to try live events instead (the get assigned to objects not yet created as well), and see if it works:
    Code:
    $('.delete').live('click',function(){
      var deleteid = this.id;
      alert(deleteid);
    });
    This does not actually delete anything.. But since I don't know if you want to delete things in list, templist or just a dom element...

  • Users who have thanked ironboy for this post:

    JustBob (09-28-2011)

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks! live() solved the problem.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by JustBob View Post
    My suspect is that the class Im trying to access comes from an associative array.
    note: there is no such thing as an associative array in JavaScript. typically you’d use a standard object (of type Object) for this purpose.


    PHP Code:
    // proof
    var = new Array;
    a["one"] = 1;
    a["two"] = 2;
    alert(a.length); // 0 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •