Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

303
Views
Append in <li><input/></li> works just one time

I would like to make a li inputable after the page being execute. I want the li function close to word office format. I mean, when we write on first list then ENTER it, it would focus on second list and ect. and this is my code I've made before:

<div class="writeform">
    <label class="subtitle">ingredients</label>
    <ol>
        <li><input class="formxyz globalwrite" id="ingred" name="ingred" placeholder="ingredients"></input>
        </li>

    </ol>
</div>

and this is the script in ajax

$('input#ingred').on('keydown',function(e){
    if(e.which == 13) {
        $('ol').append('<li><input class="formxyz globalwrite" id="ingred" name="ingred" placeholder="ingredients"></input></li>');
        $('input#ingred').focus();
    }
});

when I run this code, and write in the 1st line then I press ENTER, the second li shows up. But when I write in 2nd li and press enter, nothing appended. How can I make this append function work continously when I press ENTER key?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You cannot have duplicate uses of ID values - it is not valid HTML, and your selector for the input element would not work properly. The same thing would be true with using a class, even though it is valid to have multiple uses of a class, the selection would select all matching elements. You would need to generate a unique ID, or use a different selector to select the last input element, such as the jquery .last() selector.

over 4 years ago · Santiago Trujillo Report

0

If there are two ID selectors of the same name , jQuery only select the first one.

over 4 years ago · Santiago Trujillo Report

0

You should bind the keydown event using delegation method, like this:

Change this:

$('input#ingred').on('keydown',function(e) {

Into this:

$(document.body).on('keydown','input#ingred',function(e) {

Because the first syntax will not bind to elements which are created later (dynamically).

UPDATE

For more correct way, you should make it bind to class or name attribute rather than id, because id id originally for naming elements and should be unique.

I think this is what you need in correct way: :D

$(document.body).on('keydown','input.formxyz',function(e) {

or:

$(document.body).on('keydown','input[name="ingred"]',function(e) {

Good luck!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!