$(".convertUnits").on('keyup blur', function(event){
var a=$(this).val();
$('#test2').val(a);
});
function addShelfFields(){
$('#test2').val('');
var number=$('#test1').val(), // Generate a dynamic number of inputs
container=$('#container')[0], // Get the element where the inputs will be added to
e=$.Event("keyup", {keyCode: 13});
while (container.hasChildNodes()){ // Remove every children it had before
container.removeChild(container.lastChild);
}
for (i=0;i<number;i++){
container.appendChild(document.createTextNode("Shelf " + (i+1) + ": ")); // Append a node with a random text
var input=document.createElement("input"); // Create an <input> element, set its type and name attributes
input.type="text";
input.id="addedShelf" + (i+1);
input.name="addedShelf" + (i+1);
input.className="addedShelvesInput ui-corner-all convertUnits";
container.appendChild(input);
container.appendChild(document.createElement("br")); // Append a line break
}
$("#container").on('keyup blur', '.convertUnits', function(event){
if (event.which==13){
$("#test1").trigger(e);
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<input class="convertUnits" type="text" id="test1" name="test1">
<input type="text" id="test2" name="test2">
<button type="button" id="build" onclick="addShelfFields();">Add number</button>
<div id="container"></div>
EDIT: I finally figuerd out how to do what I wanted... I'm leaving everything here maybe it can help someone struggling with the same problem. I updated the code snippet. The added input is triggered by enter key press.
The biggest mistake I was making is the placement of
$("#container").on('keyup blur', '.convertUnits', function(event){});
it was in the for
loop. Once I realised this I moved it outside the for
loop and got the rest to work.
I'm dynamically creating inputs like this which does what I want. But the newly created inputs don't inherit the functionality or styling of the other inputs. I got around the css with the included css code and I was able to add on keypress. But there's more complex function calls that I can't get to work along with some validation from jquery validate.
I add the convertUnits
class but it doesn't work like it should.
$(".convertUnits").on('keyup blur', function(event){do stuff here)};
In short is it possible to link the new inputs to the css and js files so that the new inputs act like the exisitng inputs?
Hopefully this makes sense...
javascript:
function addShelfFields(){
var number=$('#cabShelves').val(),
container=$('#container')[0];
while (container.hasChildNodes()){
container.removeChild(container.lastChild);
}
for (i=0;i<number;i++){
container.appendChild(document.createTextNode("Shelf " + (i+1) + ": "));
var input=document.createElement("input");
input.type="text";
input.id="addedShelf" + (i+1);
input.name="addedShelf" + (i+1);
input.className="addedShelvesInput ui-corner-all";
input.onkeypress=validate_numbers;
container.appendChild(input);
container.appendChild(document.createElement("br"));
}
css:
.addedShelvesInput{
padding-top: 0px;
padding-bottom: 0px;
margin-bottom: 2px;
width: 110px;
}