JQuery append () only works for the first time

A strange problem. I have the following code:

// remove template row from DOM and store in variable

var $addDegreeRow = $('.addDegreeRow').remove();
$('#addDegreeLink').click(function (ev) {
    ev.preventDefault();
    $('.degreeSection').append($addDegreeRow);
});

I select an element from the DOM by deleting it and storing its contents in the selector $addDegreeRow. Then I assign a click event to the link on the page that adds the saved selector to the existing div on the page.

The first time I click on an event, everything works fine. But the second time through nothing is added. And if I look at the HTML in firebug, the div .degreeSectionwill be highlighted every time I click, even if nothing is added. How jQuery rewrites an old $addDegreeRowappend with a new one, rather than just adding another one.

Has anyone encountered this problem before? Thank.

+3
1

. , ().

+11

Source: https://habr.com/ru/post/1773412/


All Articles