Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

jQuery: adding rows to table: How to concatenate index to id and name elements

 
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 table that is built from a database, so its size is always variable depending on the account being requested. Once the page is rendered I use JQuery to add additional rows when the user clicks on a button. I want to use an index at the end of the ID and Name element, so that I can identify what row is being manipulated later in some other JQuery Logic and in the backend when a JAVA Servlet gets the parameter names. (I.e. id="requestedInput" + rowCount = requestedInput1 or requestedInput2 etc. )
I’ve tried the below and a few other failed attempts, any wisdom?
 
Sheriff
Posts: 67537
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
A few things stick out to me:
  • Initializing rowCount to a space is bizarre, unnecessary, and confusing. In fact, its existence outside the handler is needless, and I'd say incorrect.
  • Your text blob of HTML has rowCount embedded as just part of the string. I assume you meant to use concatentation to assemble the final string interspersed with the value of rowCount. If so, you need to get the syntax right. E.g. 'string' + value + 'string', and not "string + value + string'
  • HTML blobs suck. If you are going to use one, at least format and indent it so that it is readable.


  • Please ask for clarifications as necessary. You're really close.
     
    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
    I didn't like the HTML blob idea either, but I searched the internet for an example of adding elements and that was one of the more popular examples. Can you give me a better idea, point me to a site or book?
    I've read the Head First JQuery book all the way thru, but some things I'm trying to do. Are un-ordinary?
     
    Bear Bibeault
    Sheriff
    Posts: 67537
    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
    I haven't ready that book, but element creation is a fundamental part of jQuery, and not in the least out of the ordinary. It's covered pretty well in my jQuery in Action series.

    Small example:

    It can get rather lengthy, but is also well-suited to abstractions and functional building of elements. But the big advantage is that there are no hard-to-read HTML blobs, and you can set attributes, properties and values via code easily.

    P.S. Though, I will admit to using HTML blobs for short, static constructs at times. But never for anything like what you are doing.
     
    This guy is skipping without a rope. At least, that's what this tiny ad said:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic