简体   繁体   中英

Jquery click event is not working properly

I want a new text input box will be appended when i click onto the latest generated text box.

But It generates new text input box only when i click the first text input box. So, any solution?

<script type="text/javascript">
$(function(event){
  $('.add-new').click(function(){
    $('.add-new').removeClass();
    $('form').append("<br><input type='text' name='user[]' class='add-new'/>");      
  });
});

</script>

<div>
  <form method='post' name='login'>
    <input type='text' name='user[]' class='add-new'/>
  </form>
</div>
$('form').on('click','.add-new', function(){

Direct event => Delegate Event

Live DEMO

Full code:

$('form').on('click', '.add-new', function() {
    $(this).removeClass('add-new');
    $(this).closest('form').append("<br><input type='text' name='user[]' class='add-new'/>");
});​

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time . By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers.

on docs

$('form[name=login]').on('click', '.add-new', function() {
    $(this).removeClass(); // $(this) instead of $('.add-new') 
                           // $(this) point to the clicked element
                           // which you want

    $('form').append("<br><input type='text' name='user[]' class='add-new'/>");
});

As you're changing the class name add-new and append new element dynamically with same class, so you need delegate event.

Read more about .on()

Note

syntax of .on() for delegate event

$(container).on(eventName, target, handlerFunction)

demo http://jsfiddle.net/JuvwB/8/

Plz note: you should use $(this) as your even is bind to .add-new already.

rest all the above are nice solutions,

Hope this helps, cheers

code

$(function(event){
  $('.add-new').on('click', function(){
    $(this).removeClass();
    $('form').append("<br><input type='text' name='user[]' class='add-new'/>");      
  });
});​

The reason why this doesn't work is because when you set the 'click' event your target doesn't exist, so no 'click' event is bound to it.

jQuery has a fancy function called the 'on' function that catches bubbling events.

$(document).on('click','.add-new', function(){

}

All events (click, mouseover, etc) start in the deepest node and bubble up through the html tree until the document. It is safe for you to catch them in the document, unless you explicitly call "stopPropagation" or return false on a processed in the middle of the bubling click handler function.

You can also catch it in the middle of the tree with $("form").on... or even $("div").on...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM