1

i want to create a dynamic form like the user needs to add attribute but those attribute not defined previously he can add as many he can for example he created a dress he want to add attribute like size he will click on add attribuute that will generate an html i have did this but there i need to insert some variable so i can change the name of the attributes

var i =1;
function add_fields() {
    var emptydiv = document.createElement('div')
    emptydiv.innerHTML = '<div class="col-12 col-md-6"><div class="group"><input type="text" name="`{i}`" id="Royalties" required><span class="highlight"></span><span class="bar"></span><label>Attributes</label></div></div><div class="col-12 col-md-6"><div class="group"><input type="text" name="Size" id="Size" required><span class="highlight"></span><span class="bar"></span><label>value</label></div></div></div>'
    document.getElementById('addable').appendChild(emptydiv);
    i++;
    console.log(i)
    return emptydiv;     
    // console.log("Hello world")                   
}

what i want to change the name fields in this html by the variable i

5
  • 1
    There's no such thing. Either you find the element(e.g getElementById, querySelector, ...) and change the attribute or completely replace it. note: id should be unique per document (id="Royalties" add numbers to it or use classes instead). But if you want to render HTML from a string just use template literal instead of regular single quote. like var a = `<input name="${somevar}">` Commented Dec 23, 2021 at 8:44
  • can you tell what exact i need to do to solve this issue Commented Dec 23, 2021 at 9:12
  • thank yo u brother now my code is working Commented Dec 23, 2021 at 9:16
  • you can answer now i will vote it Commented Dec 23, 2021 at 9:17
  • Just added fixed version of your snippet Commented Dec 23, 2021 at 9:20

1 Answer 1

1

I believe this is what you are trying to accomplish.

I changed regular single quotes into a template literal

var i =1;
function add_fields() {
    var emptydiv = document.createElement('div')
    emptydiv.innerHTML = `<div class="col-12 col-md-6"><div class="group"><input type="text" name="${i}" required><span class="highlight"></span><span class="bar"></span><label>Attributes</label></div></div><div class="col-12 col-md-6"><div class="group"><input type="text" name="Size" id="Size" required><span class="highlight"></span><span class="bar"></span><label>value</label></div></div></div>`
    document.getElementById('addable').appendChild(emptydiv);
    i++;
    console.log(i)
    return emptydiv;     
    // console.log("Hello world")                   
}

add_fields();
<div id="addable"></div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.