Array.push in javascript not working

var conversations = new Array();
jQuery('.CChatWindow').each(function(){
    if (jQuery(this).is(":visible") && jQuery(this).attr("data-conversationid") != 0) {
        alert(jQuery(this).attr("data-conversationid")); // returns 1 and 2
        conversations.push = (jQuery(this).attr("data-conversationid"));
    }
});
alert(conversations); // returns an empty string

What is the problem with my code? array.push doesn't seem to work. Thank!

+3
source share
2 answers

Change

conversations.push = (jQuery(this).attr("data-conversationid"));

For

conversations.push( jQuery(this).attr("data-conversationid") );

Array.push() - a function call, not an assignment.

+8
source

array.pushis a function. Use it as:

conversations.push(jQuery(this).attr("data-conversationid"));
+3
source

All Articles