Having some trouble figuring out how to incorporate multiple functions with variables in a single JavaScript file. The syntax is throwing me off.
I'm attempting to follow an example from randomsnippets.com but would like to expand it to allow for additional sections of the form to be added dynamically. I want to avoid cluttering my code with numerous script tags or separate JS files!
In summary, here's what I'm aiming for:
variable a = value
variable b = value
function name(something){
// operations involving variable a
}
function name(something){
// operations involving variable b
}
Any guidance on accomplishing this would be greatly appreciated!
<script>
var counter = 1;
var limit = 3;
function addInput(divName){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
</script>
<form method="POST">
<div id="dynamicInput">
Entry 1<br><input type="text" name="myInputs[]">
</div>
<input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</form>