How to get text field values ​​from a dynamically generated form to a meteor, while the number of text fields is not fixed

I want to get the value of text fields from a dynamically created form and save it in the database. The number of text fields is not fixed. The form is inside the template.

<template name="product"> <input type="text" id="txt1"> <input type="text" id="txt2"> ....... ....... <button id="CreateNewProduct">Create new product</button> </template> 

I want to insert text box values ​​into the product collection. I just showed two text fields here for reference. It can be 3,4,5, ...... up to n numbers. It may also contain a flag and a radio.

Can you suggest me how to do this?

+6
source share
1 answer

You will need to iterate over the existing input elements and match their corresponding id with their n th, standing in the form as a link for a specific property of the Product collection.

 Template.product.events({ "submit form": function(event, template) { var product = {}; template.$("input").each(function(index) { product["property"+index] = template.$("input#txt"+index).val(); }); Product.insert(product); } }) 
0
source

Source: https://habr.com/ru/post/987852/


All Articles