0

i have div for each user appended to onlineusers and when specific user leave i want to delete appended div of him

im adding online users this way:

 for(var i=0;i < data.length; i++) {
 $('#onlineusers').append('<div class="user" data-id="' + data[i].id + '"><span>' + data[i].jmeno + '</span></div>');
 }

for example i want to remove user with data-id 60 so i tried

 $('#onlineusers').remove('<div class="user" data-id="60"></div>');

Thanks for any help

asked Nov 21, 2013 at 9:38

3 Answers 3

2

You call remove() on the jQuery collection you want to remove, not the parent. You can use the attribute selector to do so:

$('div.user[data-id=60]').remove();

You could also use filter() instead of filtering your selector, say if you wanted to change the .user elements prior to removing one with a specific data-id:

$('div.user').filter('[data-id=60]').remove()

JSFiddle

animuson
54.9k28 gold badges142 silver badges150 bronze badges
answered Nov 21, 2013 at 9:39
Sign up to request clarification or add additional context in comments.

1 Comment

+1 "...on the selector..." On the jQuery set you've created using that selector.
1

You can try using the attribute selector

$('.user[data-id="60"]').remove()
answered Nov 21, 2013 at 9:39

Comments

0
`for(var i=0;i < data.length; i++) {
 $('#onlineusers').append('<div class="user" data-id="' + data[i].id + '"><span>' + data[i].jmeno + '</span></div>');
 }`

i would change this to this .

`for(var i=0;i < data.length; i++) {
 $('#onlineusers').append('<div class="user" id="' online+ data[i].id + '"><span>' + data[i].jmeno + '</span></div>');
 }`
 `$('#online60').remove();`
answered Nov 21, 2013 at 9:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.