Selector doesn't work after appending inside .on() click event


Christian Burgos

The class selector dynamically generated with my jquery function doesn't work.

Here is my jQuery:

$(".add-category").on("click",function () {
        // var id = $('#row-panel #col-panel').length.toString();
        var id2 = ($('#parent-row #child-row').length + 1).toString();

        $('#parent-row').append('<div class="row p-10" id="child-row">\
                                    <div class="col-lg-12">\
                                      <div class="panel panel-default panel-grey">\
                                        <div class="panel-body">\
                                          <div class="row p-10">\
                                            <div class="col-lg-4" >\
                                             <label>Category Name</label>\
                                            </div>\
                                            <div class="col-lg-8">\
                                              <input type="text" class="form-control" id="website-url">\
                                            </div>\
                                          </div>\
                                          <div class="row p-10">\
                                            <div class="col-lg-4" >\
                                             <label>Category URL</label>\
                                            </div>\
                                            <div class="col-lg-8">\
                                              <input type="text" class="form-control" id="website-url">\
                                            </div>\
                                          </div>\
                                        </div>\
                                      </div>\
                                    </div>\
                                  </div>\
                        ');
                    });

    $(".remove-category").on("click",function () {
        if ($('#parent-row #child-row').length == 1) {
            alert("You cannot remove the last form!");
            return false;
        }
        $("#parent-row #child-row:last-child").remove();
    });

this is the mark

<div class="panel-body" id="parent-row">
                                  <div class="row p-10">
                                    <div class="col-lg-4">
                                      <label>1 Category</label>
                                    </div>
                                    <div class="col-lg-8">
                                      <input type="text" class="form-control">
                                    </div>
                                  </div>
                                  <div class="row">
                                    <div class="col-lg-offset-6 col-lg-4">
                                      <label>Add Sub Panel</label>
                                    </div>
                                    <div class="col-lg-2">
                                      <a href="#" id="add-category" class="add-category">
                                        <i class="fa fa-plus"></i>
                                      </a>&nbsp;&nbsp;&nbsp;
                                      <a href="#" id="remove-category" class="remove-category">
                                        <i class="fa fa-times"></i>
                                      </a>
                                    </div>
                                  </div>
                                  <div class="row p-10" id="child-row">
                                    <div class="col-lg-12">
                                      <div class="panel panel-default panel-grey">
                                        <div class="panel-body">
                                          <div class="row p-10">
                                            <div class="col-lg-4" >
                                             <label>Category Name</label>
                                            </div>
                                            <div class="col-lg-8">
                                              <input type="text" class="form-control" id="website-url">
                                            </div>
                                          </div>
                                          <div class="row p-10">
                                            <div class="col-lg-4" >
                                             <label>Category URL</label>
                                            </div>
                                            <div class="col-lg-8">
                                              <input type="text" class="form-control" id="website-url">
                                            </div>
                                          </div>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                </div>

The div with the id parent row is inside the dynamically generated dynamic div.

Here is the jsfiddle : http://jsfiddle.net/fn51hrgv/2/

EDIT: This is the latest code now and it works perfectly based on how I want it to behave. http://jsfiddle.net/fn51hrgv/4/

Reana

Try something like this:

 $(".panel-body").on("click", ".remove-category",function () {

Related


jQuery click event doesn't work after adding method

Edward So I have this button, which adds a new row to the table, but my problem is that .new_participant_formafter the append method is executed , it no longer listens to the click event. http://jsfiddle.net/cTEFG/ Click Add New Entry, and then click the form

Not a selector, the on() click event doesn't work

Cesar Bielich i have my html <tr class="show_ticket_preview"> <td> <a class="ticket_preview_id">A Link</a> <p>Some other stuf</p> </td> </tr> I need to fire the click event on click, trnot when a is <a>clickedtr $(document).on('click

.click doesn't work after appending

Jack For example, I append li after ul: $(".vocab-list").append( $('<li onclick="play_sound(\''+val['audioURL']+'\');" class="vocab-word" id="vocab_' + val['id']+ '"><img width="230px" height="230px" src="' + val['imageURL'] + '" /><div><i class="fa fa-play-ci

jQuery - click event doesn't work after resizing

hang I'm making a responsive menu that checks for the window.resize event to allow the functionality of a button to be clicked when it fits the browser's minimum width. If the browser is wider, the click functionality will not be limited. I need to do this bec

jQuery - each loop doesn't work inside click event

Fast bad I ran into this problem. I've been searching the internet for about 2 hours before asking this question, and I've also searched here on stack overflow. I have a .click event on the navigation scope and I link them to the content div by setting the sam

Legend Click event doesn't work after iteration

Kamal I am facing an issue with dimple.js. I have created a stacked chart and would like to bind a click event to each legend of the chart. If I reload the page, it works fine, but when I change the chart data via a function call, it changes the chart content

Not a selector, the on() click event doesn't work

Cesar Bielich i have my html <tr class="show_ticket_preview"> <td> <a class="ticket_preview_id">A Link</a> <p>Some other stuf</p> </td> </tr> I need to fire the click event on click, trnot when a is <a>clickedtr $(document).on('click

Click event doesn't work in PrimeNg listbox inside template

Kushal Baldev I'm trying to add a click event inside a listbox template in prime Ng, but the stackblitz code and demo link below is not listening to the event. Below is the code <h5>Single</h5> <p-listbox [options]="cities" [(ngModel)]="selectedCity" option

Click event doesn't work in PrimeNg listbox inside template

Kushal Baldev I'm trying to add a click event inside a listbox template in prime Ng, but the stackblitz code and demo link below is not listening to the event. Below is the code <h5>Single</h5> <p-listbox [options]="cities" [(ngModel)]="selectedCity" option

Click Event inside jQuery plugin on another element doesn't work

MyXoToD I found many articles on this topic on stackoverflow, but no one gave me a solution to my exact problem. I have a jQuery plugin that works well so far. In this plugin, I have added some new content DIVs. I also want to add a click event listener on the

Click event doesn't work inside handlebar template

Harry My template is like this {{#users}} <div id="userRoleInfo"> <input type="hidden" id="userId" value="{{id}}" /> <div class="label"> <label>User permission</label> </div> <div class="user-item-right"> // I want to

jQuery selector doesn't work after appending

Craig Rafferty I have a php script that returns html directly in the container or body and works fine (EX '<image id="trolleyLogoEdge" class="pictureFrame party" src="tipsyTrixy.png" >') . Appending this text to the divselector $('#pictureFrame > img:first')wi

jQuery selector doesn't work after appending

Craig Rafferty I have a php script that returns html directly in the container or body and works fine (EX '<image id="trolleyLogoEdge" class="pictureFrame party" src="tipsyTrixy.png" >') . Appending this text to the divselector $('#pictureFrame > img:first')wi

After clearing and appending html, click doesn't work

Aiyu After I clear and use jQuery to append the html inside the div, the click doesn't work. Here is the HTML code <div id="divMain"> </div> <input id="btn" type="button" value="Clear&Add"/> Here is the jQuery code var a = $('<a/>').attr({'id':'aH','href':'#'

.click doesn't work after appending

Jack For example, I append li after ul: $(".vocab-list").append( $('<li onclick="play_sound(\''+val['audioURL']+'\');" class="vocab-word" id="vocab_' + val['id']+ '"><img width="230px" height="230px" src="' + val['imageURL'] + '" /><div><i class="fa fa-play-ci

Click and selector: no - doesn't work

Alex Why selector: no - doesn't work? After clicking on the red area, open the alert. Why? <div class='content'> <div style="width:200px; height:200px; background:#EEE;"> <div id="click" style="width:100px; height:100px; background:red"></div>

Scroll event after appending item doesn't work

bank Tried to call scroll event after appending items but it doesn't work. What did i do wrong? Please help me thanks. You can view the code here https://jsfiddle.net/4uk92cxe/1/ $(document).ready(function () { $('#myDiv2').append( "<table class='scroll-item

.click doesn't work after appending

Jack I append li to ul like: $(".vocab-list").append( $('<li onclick="play_sound(\''+val['audioURL']+'\');" class="vocab-word" id="vocab_' + val['id']+ '"><img width="230px" height="230px" src="' + val['imageURL'] + '" /><div><i class="fa fa-play-circle"></i>'

Not a selector, the on() click event doesn't work

Cesar Bielich i have my html <tr class="show_ticket_preview"> <td> <a class="ticket_preview_id">A Link</a> <p>Some other stuf</p> </td> </tr> I need to fire the click event on click, trnot when a is <a>clickedtr $(document).on('click

Click event doesn't work in PrimeNg listbox inside template

Kushal Baldev I'm trying to add a click event inside a listbox template in prime Ng, but the stackblitz code and demo link below is not listening to the event. Below is the code <h5>Single</h5> <p-listbox [options]="cities" [(ngModel)]="selectedCity" option

Click event doesn't work in PrimeNg listbox inside template

Kushal Baldev I'm trying to add a click event inside a listbox template in prime Ng, but the stackblitz code and demo link below is not listening to the event. Below is the code <h5>Single</h5> <p-listbox [options]="cities" [(ngModel)]="selectedCity" option

Click event doesn't work in PrimeNg listbox inside template

Kushal Baldev I'm trying to add a click event inside a listbox template in prime Ng, but the stackblitz code and demo link below is not listening to the event. Below is the code <h5>Single</h5> <p-listbox [options]="cities" [(ngModel)]="selectedCity" option

Click event doesn't work in PrimeNg listbox inside template

Kushal Baldev I'm trying to add a click event inside a listbox template in prime Ng, but the stackblitz code and demo link below is not listening to the event. Below is the code <h5>Single</h5> <p-listbox [options]="cities" [(ngModel)]="selectedCity" option

After clearing and appending html, click doesn't work

Aiyu After I clear and use jQuery to append the html inside the div, the click doesn't work. Here is the HTML code <div id="divMain"> </div> <input id="btn" type="button" value="Clear&Add"/> Here is the jQuery code var a = $('<a/>').attr({'id':'aH','href':'#'

.click doesn't work after appending

Jack I append li to ul like: $(".vocab-list").append( $('<li onclick="play_sound(\''+val['audioURL']+'\');" class="vocab-word" id="vocab_' + val['id']+ '"><img width="230px" height="230px" src="' + val['imageURL'] + '" /><div><i class="fa fa-play-circle"></i>'