Click here to Skip to main content
11,803,495 members (70,965 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Javascript HTML5
how to display the newest index of an array in javascript?
we enter multiple text in our textbox made in HTML,and that is stored
in array and we want those entered text to be displayed ,where newly entered has to be on top?
Posted 10-Apr-13 22:43pm
enhzflep at 11-Apr-13 4:48am
The newest item will be the one with the highest index.

var lastItem = arrayObj[ arrayObj.length - 1];
Matej Hlatky at 11-Apr-13 5:06am
Array Object has the length property, not size.
enhzflep at 11-Apr-13 5:09am
fixed. Thanks.
umeshinator at 11-Apr-13 5:12am
can u please explain a bit? thank you in advance
enhzflep at 11-Apr-13 5:24am
Can I explain a bit about what?
umeshinator at 11-Apr-13 5:33am
also,how to store the values in the array ?
i have created an array like this ,is it correct?

var msg = new array();
var lastitem = msg[msg.length -1];
enhzflep at 11-Apr-13 5:54am
It's close, but javascript is CaseSensitive. (array should be Array)

Here's a short demo:
function demoFunc()
var i, mArray = new Array();
for (i=0; i<10; i++)
mArray[ i ] = "Int: " + i;

for (i=0; i<10; i++)
mArray[ mArray.length ] = "Float: " + (0.5+i);

var ul = document.createElement('ul');
var len = mArray.length;
for (i=0; i<len; i++)
var li = document.createElement('li');
li.appendChild( document.createTextNode( mArray[i] ) );

Visis followed by


( do read, it's enlightening)
umeshinator at 11-Apr-13 14:08pm
thankyou bro,but y u against w3schools..i thik w3schools preety gud
enhzflep at 11-Apr-13 14:15pm
No worries mate.

Why I'm against w3schools?
I've noticed incomplete, trivial examples in far too many cases. I could go on and on and on, but the article I linked to will do a far better job than I can.

I've fixed the links in my above comment, and will repeat one.

I really suggest you read it. (I'd throw rocks at if they were in range)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try this:

var registeredElements = [];

// enter data in array

var LastArrayIndexValue = regManadatoryElements[regManadatoryElements.length];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 CPallini 290
2 F-ES Sitecore 270
3 Sergey Alexandrovich Kryukov 234
4 CHill60 220
0 OriginalGriff 3,000
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,755
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100