Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

337
Views
is there a way to run javaScript in th:action method thymeleaf

This is my form:-

<form action="#" th:object="${note}" th:action="$('#note-id').val()!=null ? @{/update} : @{/note}" method="POST">
    <input type="hidden" name="noteId" id="note-id" th:field="*{noteId}"  >
    <div class="form-group">
       <label for="note-title" class="col-form-label">Title</label>
       <input type="text" name="noteHead" th:field="*{noteHead}"  class="form-control" id="note-title" maxlength="20" required>
    </div>
    <div class="form-group">
       <label for="note-description" class="col-form-label">Description</label>
       <textarea class="form-control" name="noteDescription" th:field="*{noteDescription}" id="note-description" rows="5" maxlength="1000" required></textarea>
   </div>
   <button id="noteSubmit" type="submit" class="d-none" ></button>
</form>

This form is inside a popUp window(modal class). I want use the same Window for add/update. Add is working fine.

For Update i am passing in the data to the modal, which is getting displayed aswell.

<button type="button" class="btn btn-success"
    th:data-noteId="${note.noteId}"
    th:data-noteTitle="${note.noteHead}"
    th:data-noteDescription="${note.noteDescription}"
    onclick="showNoteModal(
    this.getAttribute('data-noteId'),
    this.getAttribute('data-noteTitle'),
    this.getAttribute('data-noteDescription'));">
Edit</button>

What i want to do is to create a conditional statement in th:action based on wheather "note-id" has value, in which case its an update otherwise it should be a New Entry.

Please Help

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You want to update th:action of your form in such a way that if note-id has value an edit request is sent to server otherwise add request?

If that's the case then you can do it this way

<form th:action="${user.note_id}==null? @{/save} : @{/edit}">

With param

<form th:action="${user.note_id}==null? @{/save} : @{/edit/(noteId=${user.note_id})}">


Check thymeleaf standard URL syntax documentation for more details.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!