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

159
Views
How to Replace All __prefix__ Placeholder Strings within Django Formsets empty_form Elements Using Only Javascript?

Django formsets have an empty_form attribute.

empty_form

BaseFormSet provides an additional attribute empty_form which returns a form instance with a prefix of __prefix__ for easier use in dynamic forms with JavaScript.

The Django documentation doesn't actually say how to replace the __prefix__ with Javascript. Several online examples show how to do it with jQuery, but I specifically want to do it with Javascript - no jQuery.

Here is the resulting HTML from my {{ formset.empty_form }}:

<div id="prerequisiteEmptyForm">
    <input type="text" name="prerequisites-__prefix__-text" maxlength="100" id="id-prerequisites-__prefix__-text">
    <label for="id-prerequisites-__prefix__-DELETE">Delete:</label>
    <input type="checkbox" name="prerequisites-__prefix__-DELETE" id="id-prerequisites-__prefix__-DELETE">
    <input type="hidden" name="prerequisites-__prefix__-id" id="id-prerequisites-__prefix__-id">
    <input type="hidden" name="prerequisites-__prefix__-content" value="21" id="id-prerequisites-__prefix__-content">
</div>

Everywhere it shows __prefix__, I want to replace it with a number... let's say 321.

Correct solution:

<div id="prerequisiteEmptyForm">
    <input type="text" name="prerequisites-321-text" maxlength="100" id="id-prerequisites-321-text">
    <label for="id-prerequisites-321-DELETE">Delete:</label>
    <input type="checkbox" name="prerequisites-321-DELETE" id="id-prerequisites-321-DELETE">
    <input type="hidden" name="prerequisites-321-id" id="id-prerequisites-321-id">
    <input type="hidden" name="prerequisites-321-content" value="21" id="id-prerequisites-321-content">
</div>

So my question becomes

Using Javascript only, how do I replace a constant value ("__prefix__") with something else ("321") across several elements (inputs and labels) within multiple attributes (name, id)? Specifically, I want to do it cleanly for repeatability. I don't want a highly custom solution to this specific problem. It needs to be a general approach... since this is replacing a constant, surely Javascript has a clean way to do this? I'm still learning Javascript and trying to not be so dependent on jQuery.

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

0

I used this concept:

const emptyForm = document.querySelector('#prerequisiteEmptyForm');
clone = emptyForm.cloneNode(deep=true);
clone.innerHTML = clone.innerHTML.replace(/__prefix__/g, '321');
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!