That's because you have that empty <li style="display:none"></li>. Is it really necessary?
just decrement the index to start at 0: $('#Themes li').each(function(index){ index--; $('input:hidden', this).each(function(){ var fixName = $(this).attr('name').replace(/\[\d\]/, '['+index +']'); $(this).attr('name', fixName); }); }); cheers, - ricardo On Feb 17, 10:56 pm, shapper <mdmo...@gmail.com> wrote: > I added an input which shows the number of items in the list ... > > The problem is that I am not able to reorder the list so it is 0 > based ... > > Any idea? > > Thanks, > Miguel > > On Feb 18, 1:48 am, shapper <mdmo...@gmail.com> wrote: > > > I have been trying to make this work but no success ... > > ... in fact the inputs get undefined. > > > I am not sure if I am using your function on the right way. > > The function should be called, I think, after adding or removing an > > item to reorder the ids. > > > Anyway, I created an example:http://www.27lamps.com/Beta/List/List.html > > > Please, not that you have to select a subject and at least one level > > to add it to the list. > > > Could someone, please, help me out with this? > > > Basically, every time I add or remove an item I need to reorder the > > names of the input fields. > > > Please check my original message on this post. It explains the > > situation. > > > Thanks, > > Miguel > > > On Feb 12, 4:36 am, Ricardo Tomasi <ricardob...@gmail.com> wrote: > > > > Probably not the most efficient function, but should work: > > > > $('#ThemesList li').each(function(index){ > > > $('input:hidden', this).each(function(){ > > > var fixName = $(this).attr('name').replace(/\[\d\]/, '['+index > > > +']'); > > > $(this).attr('name', fixName); > > > }); > > > > }); > > > > - ricardo > > > > On Feb 11, 7:19 pm, shapper <mdmo...@gmail.com> wrote: > > > > > Hello, > > > > > I am adding and removing a items from a list, using JQuery, which is > > > > rendered has follows: > > > > > <ol id="ThemesList"> > > > > <li"> > > > > <input type="hidden" name="Themes[0].Subject" value="A" /> > > > > <input type="hidden" name="Themes[0].Levels" value="L1,L2" /> > > > > <input type="hidden" name="Themes[0].Description" value="Paper" /> > > > > </li> > > > > <li"> > > > > <input type="hidden" name="Themes[2].Subject" value="B" /> > > > > <input type="hidden" name="Themes[2].Levels" value="L1,L5" /> > > > > </li> > > > > <li"> > > > > <input type="hidden" name="Themes[5].Subject" value="D" /> > > > > <input type="hidden" name="Themes[5].Levels" value="L2,L4" /> > > > > <input type="hidden" name="Themes[5].Description" value="Book" /> > > > > </li> > > > > </ol> > > > > > Every time I add or remove a Theme I need to be sure that the list is > > > > ordered (name) starting with Themes[0]. > > > > > So basically I need to loop through each list item in list ThemesList. > > > > - In first list item all HIDDEN inputs names should start with "Themes > > > > [0]" > > > > - In second list item all HIDDEN inputs names should start with "Themes > > > > [1]" > > > > ... > > > > > So in this example, "Themes[2]. ..." would become "Themes[1]. ..." and > > > > "Themes[5]. ..." would become "Themes[2]. ..." > > > > > Could someone please help me out? > > > > > I have no idea how to do this. > > > > > Thanks, > > > > Miguel