#1
  1. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,997
    Rep Power
    375

    Jquery .on isnt working on button click


    Ok I have a page,where users can put in some texts, click add more and another text box appears along with "remove" button.

    When i click this remove button, JQUERY on function isn't working for some reason. I say this because I have an alert("2"); which doesnt appear..

    Code:
    $(document).ready(function() {
        var i = 0;
        $( "#add_more" ).on('click', function() {
            
            if ( i < 4 ) {
                var input =
                '<div>' +
                    '<input class="emails" type="email" name="emails[]"  />';
                    
                input += '<span class="btn_box"> <input id="remove_'+ i +'" type="button" name="remove'+ i + '" value="Remove" class="remove"/></span>';	
                input += '</div>';
                $("#more").append(input);
                
            } else {
                alert ("There is a limit of 5 people!");
            }
            i++;
            
            
            //$("#original").clone();//
            //alert ( clone );
            //.appendTo("#more").css("display","block");
        });
        
        $(".remove").on("click", function(){
            alert("2");
            $(this).parent().parent().remove();
            i--;
            
        });
    });


    EDITED -- the .on event is messed up. changed it to live ( and used a previous version of jquery) and everything was fine
    Last edited by paulh1983; November 5th, 2013 at 05:22 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    876
    Rep Power
    275
    THISmay help you get .on() to work correctly.

    It appears you have to setup .on() differently than you would .live() by first targeting the parent element and then in the .on() function, filtering with the actual element you want to work with.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,997
    Rep Power
    375
    cheers. I was hoping someone would still put me in right direction rather than me having to go back to dark ages..

IMN logo majestic logo threadwatch logo seochat tools logo