jQuery data() is not working properly

142 views Asked by At

In my following code:

addUserHandler: function () {
    var $user = this.$('#user');
    if (($user.val().length > 0) && ($user.val() === $user.data('name'))) {
        this.addUser($user.data());                                                                                                                                                                                                                                       
    } 
    $user.val('');
    return false;
}

When I add the first user and then if I select another user from the autocomplete text field, it actually replaces the previously selected item's data with the new one. So always all the previously selected items will be replaced with the final one.

I'm using jQuery 2.0 and this issue started to occur after I updated from jQuery 1.9.

Anyone know any recent changes that jQuery has done for $.data() I tried to find some proper solutions, but couldn't found any.

Thanks

2

There are 2 answers

2
Rohan Kumar On

Change the line

var $user = this.$('#user');

to

var $user = $(this).find('#user');

or simply

var $user = $('#user');// it must be unique
0
codebreaker On

var $user = this.$('#user'); this value stays same and wherever u use it that's y it shows up the same value...