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

159
Views
When I'm clicking on input filed it works, but if I add new fields of input field the blur function on that is not working How to solve this by jquery

html code

<div id="add-code">
<input type="text" class="code">
</div>
<a id="new-code"></a>

java script code

<script>
$(document).ready(function(){$("#new-code").click(function(){$("#add-code").append(`<input type="text" class="code">`)
});
$(".code").blur(function(){$(this).remove()})
})
</script>

here I have made a on click function for my tag for adding more input fields and blur function on code class for removing that input field but as I add more fields, the new added input fields do not support blur function

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You were actually very close. You were forgetting to add the event listener to the new elements when you were creating them.

I have corrected your function here

$("#new-code")
.click(function(){
    $("#add-code").append(
        $(`<input type="text" class="code">`).blur(function(){$(this).remove();})
    );
});

$(".code")
.blur(function(){
    $(this).remove();
});

Because these elements were created after your JavaScript loaded they never get the event listener. Therefore when we create the new elements we must bind the event listener to them before appending to the container.

about 4 years ago · Juan Pablo Isaza Report

0

Because blur event listener is only called once, so it's active only on the currently existing inputs.
You need to call it each time you append a new input like this:

$(document).ready(function () {
  $("#new-code").click(function () {
    $("#add-code").append(`<input type="text" class="code">`);
    
    $(".code").last().blur(function () {
      $(this).remove();
    });
  });
});

Working example => https://codepen.io/moaaz_bs/pen/NWwGzvy?editors=1010

about 4 years ago · Juan Pablo Isaza 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!