0

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>

4 Answers 4

5
$('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

Sign up to request clarification or add additional context in comments.

1 Comment

@ShahriarMahmood. No problem. If you really want to improve your jQuery skills, read the docs in my answer. Good luck!
2

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'/>");      
  });
});​

Comments

2
$('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)

Comments

1

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...

3 Comments

$('.add-new').removeClass(); will remove all the classes from all the elements. I've a feeling this is not what the OP wants.
his question was about the click event, not about the code he had inside of it, I just copy pasted it. But I'll remove it
Now I'm happy to give my +1... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.