JavaScript/jQuery array logic not working

Eric Zwar
Eric Zwar used Ask the Experts™
on
I have a couple of PHP modules that include JS and jQuery logic.  One is chat.php and the other is fetch_user.php.  chat.php does a jQuery ajax call to fetch-user.php  and fetch_user.php returns an HTML table to chat.php which is then inserted into chat.php's DOM.  All this works perfectly.

In the data returned from fetch_user.php there is an HTML button which has some attribute data which the ajax  'success' function has access to and I can extract this data OK.

My issue is that I have a JS array which as far as I am aware has global JS access.  I want to first empty the existing data in that array (which is named logged_in_array) and the add the extracted attribute data to that array.  In my code the logged_in_array.length = 0 does not empty the array, nor does the logged_in_array.push work.

So my specific issues are that the array is not being emptied nor is the attribute datum being added to the array.

I will attach extracts of the code:

demo-code.html
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
to empty just set it value to an empty one :
logged_in_array = [];

Open in new window

or :
logged_in_array = new Array();

Open in new window

Author

Commented:
My understanding is that I can't do that as the array needs to be visible to other JS functions and if I use suggestion 1: it will create a new array not globally visible within JS.
Multitechnician
Top Expert 2014
Commented:
no, we don't redeclare/recreate the object so its scope doesn't change
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I tried that but it does not empty the array and the array push still does not work as when I call fetch_user again it is still sending the original array data



demo-code.html

Author

Commented:
Don't know why but it is now working??

Sorry to have bothered you.
leakim971Multitechnician
Top Expert 2014

Commented:
no worry :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial