'(JQuery) Output is undefined when getting a value from a dynamically created input
I am making an inventory database using JS and JQuery to firebase realtime database. The only problem I have now is when I input my values and run the function, it reads as undefined in the console log so it doesn't add data to the database and runs an error because it's undefined.
Uncaught Error: set failed: value argument contains undefined in property 'Inventory.undefined.Quantity'
This is the code to add inputs to the table:
$(".add-new").click(function(){
$(this).attr("disabled", "disabled");
var index = $("table tbody tr:last-child").index();
var row = '<tr>' +
'<td><input type="text" class="form-control" name="name" id="name"></td>' +
'<td><input type="text" class="form-control" name="unit" id="unit"></td>' +
'<td>' + actions + '</td>' +
'</tr>';
$("table").append(row);
$("table tbody tr").eq(index + 1).find(".add, .edit").toggle();
});
This is the code making the input into a variable:
$(document).on("click", ".add", function(){
var empty = false;
var input = $(this).parents("tr").find('input[type="text"]');
input.each(function(){
if(!$(this).val()){
$(this).addClass("error");
empty = true;
} else{
$(this).removeClass("error");
}
});
$(this).parents("tr").find(".error").first().focus();
if(!empty){
input.each(function(){
$(this).parent("td").html($(this).val());
});
$(this).parents("tr").find(".add, .edit").toggle();
$(".add-new").removeAttr("disabled");
let name = $(this).parents("tr").find("input[name=name]").val();
let unit = $(this).parents("tr").find("input[name=unit]").val();
console.log(name,unit);
set(ref(db, 'Inventory/' + name), {
Quantity: unit
});
}
});
This is the output for the console log:
undefined undefined
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
