I'm using the jQuery UI autocomplete widget on a text input. This text input is used to add a product to the database.
Here's my code:
HTML:
<div class="form-group">
<input type="text" class="form-control" placeholder="Termék kiválasztásához kezdjen el gépelni..." id="search">
<div id="autocomplete"></div>
</div>
JS:
$('#search').on('keypress', function () {
$(this).autocomplete({
appendTo: "#autocomplete",
autoFocus: true,
minLength: 0,
source: "array - ommited for clarity"
});
});
I would like to add a button which adds another input field, so the user can add another product to the database.
My problem would be, that if I duplicate the HTML I'm using, I'll have multiple elements with the same ID. Of course, the logical move would be to replace ID's with classes, but then again, if I'm correct the class selector for jQuery selects the first matched element, so the keypress event would fire on the original input.
How can I add another text input without duplicating my javascript code?