0

I have an HTML form that I wanna to use to generate dynamically ADD more input fields:

 <div class="row cloneDefault">
 <div class="form-group col-md-1">
 <br />
 <h4 style="text-align:right">1.</h4>
 </div>
 <div class="form-group col-md-1">
 <label class="control-label">First name</label>
 <input type="text" value='' class="form-control" id="FirstnameField"/>
 </div>
 <div class="form-group col-md-2">
 <label for="sname" class="control-label">last name</label>
 <input type="text" value='' class="form-control" id="lastnameField"/>
 </div>
 <i class="remove">remove</i>
 </div>
 <form class="frm">
 <div class="row">
 <div class="form-group col-md-1">
 <br />
 <h4 style="text-align:right">1.</h4>
 </div>
 <div class="form-group col-md-1">
 <label for="fname" class="control-label">First name</label>
 <input type="text" value='' class="form-control" id="fname"/>
 </div>
 <div class="form-group col-md-2">
 <label for="sname" class="control-label">last name</label>
 <input type="text" value='' class="form-control" id="sname"/>
 </div>
 <i class="remove">remove</i>
 </div>
 <i class="add"> Add </i>
 </form>
<style> 
.cloneDefault{
 display: none;
}
</style>

And JS to handle adding input fields dynamically:

$(document).ready(function() {
 $(".add").click(function() {
 $(".cloneDefault").clone(true).insertBefore(".frm > div:last-child");
 $(".frm > .cloneDefault").removeClass("cloneDefault");
 return false;
 });
 $(document).on('click', '.remove', function() {
 $(this).parent().remove();
 });
});

Now, I can easily retrieve text value from an input field by element id and then query cloud firestore:

const first_name = document.querySelector('#fname').value;
const last_name = document.querySelector('#sname').value;
firebase.firestore().collection("users").add({
 fame: first_name,
 sname: last_name,
}).then(function () {
 console.log("success");
 })
 .catch(function (error) {
 console.log("error:", error);
 });

But how to save dynamically generated input fields in JS and save them on cloud firestore? I realised that my code to generate dynamically added input data may be unsuitable if I'd like to use it afterwards. What I wanted to do is similiar as this . Any idea?

asked Jul 30, 2020 at 14:03
4
  • It's not clear to me what you mean by "dynamicaly". How is that different than what you have now? Please edit your question to explain the problem you're trying to solve. Commented Jul 30, 2020 at 14:54
  • Hi Doug thanks for letting me know. I have edited my question! Commented Jul 30, 2020 at 15:06
  • I think what your looking for is how to use the value of a variable as the index/field name? Not really a Firestore question, but you can use { [fieldnameVariable]: fieldvalueVariable }. Note the '[ ]' . This is basic javascript object notation. (and yes, I use exactly this in many, many places for a JS/React/Firestore application) Commented Jul 30, 2020 at 20:24
  • @LeadDreamer Hi, yes I realised this is more likely a JS data structure problem. Currently I used arrays + basic For loop to solve it, but it seems not neat. Would happy to know your solution and I will pick yours to close this post. Commented Jul 30, 2020 at 23:35

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.