I have a doubt what happens is that I am trying to extract the id of a Django formset in Javascript, however when trying to do it I realized that this appears in Chrome DevTools:
S.fn.init
[[Prototype]]: Object (0)
That is, nothing is happening, but it seems strange to me because when printing in the console I get the id name that I want
what is happening and how can I extract its id and therefore the value??
calculate.js
//the number of total forms is taken
var formId="id_form-TOTAL_FORMS"
var form_idx = document.getElementById('id_form-TOTAL_FORMS').value;
console.log("el valor es:" form_idx);
var totalForms=parseInt($('#' formId).val());
console.log("totalForms:" totalForms)
console.log(totalForms 1);
//dynamic id are created
var formset_quantity='id_form-' form_idx '-quantity';
var formset_unit_price='id_form-' form_idx '-unit_price';
console.log('formset quantity: ' formset_quantity);
console.log('formset unit price: ' formset_unit_price);
//the value of the one that contains the dynamic id is taken
//This is when everything goes wrong because when printing in the console I get the error above,
// as if that id does not exist(formset_quantity and formset_unit_price) or I am not creating it correctly
var id_formset_quantity=document.getElementById('formset_quantity').value;
var id_formset_unit_price=document.getElementById('formset_unit_price').value;
var formset_total_price=document.getElementById('id_form-' form_idx '-total_price');
presupuestos-forms.html
td id="parent_id_form-0-quantity">
<input type="number" name="form-0-quantity" class="form-control" id="id_form-0-quantity">
</td>
<td id="parent_id_form-0-unit_price">
<input type="number" name="form-0-unit_price" class="form-control" id="id_form-0-unit_price">
</td>
<td id="parent_id_form-0-total_price">
<input type="number" name="form-0-total_price" class="form-control" id="id_form-0-total_price">
</td>
CodePudding user response:
Leave out the quotation marks when you refer to a variable. Otherwise it will be interpreted as a string.
var id_formset_quantity=document.getElementById(formset_quantity).value;