• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Is It Possible To Append A ThymeLeaf Attribute With JQuery Such As th:href

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the posts I've read it doesn't seem possible to append/prepend a thymeleaf attribute using jquery such as the one below. But I'm wondering if there's any way around it. If so I'd be really grateful if you could specify how.

For example I have the below code but it isn't working because ThymeLeaf is compiled on the server while jquery is processed at the client.

Is there a way around this?

This is the code:



To elaborate on why I want to do this:

I have a table in a web application that displays the logged in users friends(left). The logged in user can add/remove friends by searching them in a search bar on the application. The below screenshot might help.



When a user searches for a friend in the right hand side search bar and adds them by clicking the heart icon I add a row to the table using jquery after acquiring the necessary data with ajax.

The problem is that this row contains thymeleaf attributes such as th:href. For example when the logged in user clicks on a friends name in the table it brings the user to the profile page of the friend.

However, thymeleaf attributes don't seem to work when appended/prepended with jquery in this way.

Would really appreciate some help.

Thanks
 
Marshal
Posts: 67510
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that with Thymeleaf, or any other server-side templating engine like JSP, what's sent to the browser is the generated HTML; and that's what any JavaScript, including jQuery, has to deal with.

As Thymeleaf attributes are a server-side mechanism, I'm not even sure why you feel you need to deal with them in client-side code. Elements you create on the fly in JS would be fully-formed without relying upon Thymeleaf to generate them.

You could, though I don't generally recommend this, have server-side components generate HTML snippets to include (rather than creating them in the JS). But you'll end up running into a lot of other issues introduced by the schism between server and client using that approach (ask me how I know -- got the battle scars).

I'd say your best bet is to become familiar with what the Thymeleaf attributes do for you, and understand that on the client you need to do the same by hand in the JS. The attributes that you used as examples do very simple things that are easy to live without on the client.
 
Bryan Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Bear thanks for the in depth answer. Very much appreciated. I'll definitely refer back to it after reading up more on thymeleaf jquery etc.
 
A tiny monkey bit me and I got tiny ads:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic