So, I've created a program that starts with one input field. By pressing the plus button, it adds new input fields with different IDs. When I press calculate, I want to save all the input field values into an array. I attempted using a for loop with .val() but it didn't work.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<title></title>
</head>
<body>
<!-- ************where the input fields are*******-->
<div id="newfields">
<p style="display:inline-block; margin-bottom:0px;">
<input type='text' class='minutes' id="minute1"/>
<input type='text' class='vidseconds' id="second1"/>
</p>
<div id="addremove">
<button id="plus">+</button>
<button id="minus">-</button>
</div>
</div>
<!-- Save data when this is pressed -->
<p id="calculation">Calculate</p>
</body>
</html>
//JavaScript
$(document).ready(function(){
var mins = [];
// Add new input field
var idnum = 1;
$("#plus").click(function(){
idnum+=1;
var newInput = "<p><input type='text' class='minutes' id='minute"+idnum+"' /><input type='text' class='vidseconds' id='second"+idnum+"'/></p>";
$(newInput).appendTo("#newfields");
});
// Remove an input field
$("#minus").click(function(){
if(idnum >= 2){
$("#minute" + idnum+ ", #second" + idnum).remove();
idnum-=1;
}
});
// Put input field data in an array on click
$("#calculation").click(function(){
});
});
/*StyleSheet */
#newfields {
display: inline-block;
}
#addremove {
display: inline-block;
}
button {
border-style: none;
background-color: #C0C0C0;
width: 24px;
height: 24px;
}
Everything is inline because I'm aiming to keep it as a single file. Comments have been added for readability.