-2

How can I remove a item/s from an Array in JavaScript ?

function restructureChatBoxes() {
    align = 0;
    for (x in chatBoxes) {
        chatboxtitle = chatBoxes[x];

        if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
            if (align == 0) {
                $("#chatbox_"+chatboxtitle).css('right', '20px');
            } else {
                width = (align)*(225+7)+20;
                $("#chatbox_"+chatboxtitle).css('right', width+'px');
            }
            align++;
        }
    }
}

And I want to remove the Chatbox when its closed from the List.

function closeChatBox(chatboxtitle) {
//HERE THE REMOVE FROM LIST ? 
    $('#chatbox_'+chatboxtitle).css('display','none');
    restructureChatBoxes();

    $.post("chat.php?action=closechat", { chatbox: chatboxtitle} , function(data){  
    });

}

Thanks for all Answers.

Simpal Kumar
  • 3,331
  • 3
  • 23
  • 47

1 Answers1

1

First, find the index of the element you want to remove:

var array = [2, 5, 9];
var index = array.indexOf(5);

Note: browser support for indexOf is limited, it is not supported in IE7-8.

Then remove it with splice:

if (index > -1) {
    array.splice(index, 1);
}

The second parameter of splice is the number of elements to remove. Note, splice modifies the array in place and returns a new array containing the elements that have been removed.

Source

Community
  • 1
  • 1
Simpal Kumar
  • 3,331
  • 3
  • 23
  • 47
  • You copied [this answer](http://stackoverflow.com/questions/5767325/remove-a-specific-element-from-an-array#answer-5767357) without any modification and without any reference to it. Post a link in a comment if you don't want to answer the question yourself. – a better oliver Jul 11 '15 at 10:23
  • Hey thanks :) But i only have the Chatboxtitle ... How can i handle it now ?:O – Varex Dev's Jul 11 '15 at 10:28
  • 1
    I have already marked this question as duplicate and why to invent the new wheel every time when information is already available round. – Simpal Kumar Jul 11 '15 at 10:30
  • He only pointed me the correct way ? and i guess thats what count or ? Nobody is searching whole stack answers ... – Varex Dev's Jul 11 '15 at 10:42
  • You don't get the point. You sold another one's answer as yours. How would you call that? And no, you didn't mark the question as duplicate. What about that? – a better oliver Jul 11 '15 at 11:16
  • I flagged it as duplicate, may be it did not get approved. and I putted as reference too. – Simpal Kumar Jul 11 '15 at 11:34