• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jQuery Remove and rename of ID

 
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a form that has mutiple input boxes that are displayed as line items. Each line item has an id that has a index at the end because the number of line items depends on the account. Each Line item has a "X" cancel image next to the input box if the use made a mistake and doesnt what to enter that line item. When the user clicks image I do a remove of that line item that works as expected. But right after the remove I try to rename the ids index and at that point all the line items are removed. I know this beause the code works with out the rename. All input elements have the class name newpAddress , which I use to rename the id. Any suggestions ?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried out just the part that change IDs and it seems to work for me. I don't really understand your description of the the problem, "I try to rename the ids index and at that point all the line items are removed." Are you saying that elements are removed from your page? Do you mean they actually don't exist in the DOM after the IDs are changed? I don't understand how that could happen.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The idea of changing ids like that makes me itch.
It just feels like something that could go quite wrong.

Log the value of the id for each of the elements you are changing before changing it.
I'm wondering if you're "overwriting" some existing id, and ending up in a mess.
I've no idea what would happen in that situation as I've never altered an id post-creation.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An id value is a value that uniquely identifies an element. The whole idea of changing them like this seems wrong and is certainly unconventional.

Why do you feel that you need to change the ids in the first place?

It's like "John went away to college so we need to rename all you remaining kids". Why?
 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your patience. The initial form as Account Text box and one address Text box, when user puts focus on 1st address box. I add plus sign so that they can add more addresses. Up to 4 more. If they click and add 4 more textboxes I now added IDs with newpAddress0 (original), then newpAddress1 , newpAddress2 and so on. Each of the 4 textboxes can be removed by clicking on cancelXid. I want to rename the IDs so that they will stay in sequence and the other code will know what to act upon (i.e. If they delete newpAddress1, I want to rename ids so there’s no gap, so newpAddress4 is now newpAddress3). Hope this more clearer
 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot to mention. I’m sure the rename is working because (debugging) and if user removes the highest address no problem. Works as expected, it’s when user removes a lower incremented text box. Using my example in previous post, if they remove newpAddress1 , then it gets removed but as everything gets renamed it loops thru and wants to delete the renamed “newpAddress1” until there are no more newpAddress1
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regardless of whether things "work" or not, your use of ids is a poor practice. If ids need to change to accommodate your design, the design is flawed.

I suspect -- but don't have enough info from the original post -- that using ids at all is the wrong approach. You should probably be exploring other ways to select the items rather than ids, which tend to be overused in situations where other selection hooks would be more appropriate.
 
Replace the word "snake" with "danger noodle" in all tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic