Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add more Adult and Child select elements when I click the + button on Rooms. I want to remove them when I click the - button, just the way I add them. I also want them to have the same functionality as the current Adult and Child elements which are present when the page is loaded.

I have included my Codepen.

Codepen[^]

What I have tried:

I tried using jQuery but I don't think its good to use both jQuery and AngularJS when it can be done using just AngularJS
Posted
Updated 21-Mar-17 4:19am
Comments
Karthik_Mahalingam 21-Mar-17 9:37am    
jquery will be easier for this
SL-A-SH 21-Mar-17 9:41am    
I know but my whole application is in AngularJS
Karthik_Mahalingam 21-Mar-17 9:45am    
ok where you got stuck ?
SL-A-SH 21-Mar-17 9:48am    
removing the elements which I add if I press the + button. The elements added should be removed if I click the - button. Check my codepen
Karthik_Mahalingam 21-Mar-17 9:49am    
you mean dynamic rows?

1 solution

Quote:
When I click the + button on Rooms, more Adult and Child select elements are added. Similarly, I want those added elements to be removed when I click the - button on rooms


$scope.minusRoom = function () {
               if ($scope.room > 1) {
                   $scope.room--;
                   $scope.price = $scope.price - 2500;
                   var div = document.querySelector('div');
                   var rows = div.querySelectorAll('.spanrow')
                   var last = rows[rows.length - 1];
                   div.removeChild(last);


               }
           }
           $scope.addRoom = function () {
               if ($scope.room < 10) {
                   $scope.room++;
                   $scope.price = $scope.price + 2500;
                   var myEl = angular.element(document.querySelector('div'));
                   myEl.append('<span class="spanrow">Adult: <input type="button"value="-"></input><input type="text" ng-value="room" readonly="readonly"><input type="button" value="+"><span> Child:<input type="button"value="-"></input><input type="text" ng-value="room" readonly="readonly"><input type="button" value="+"><br/>');
               }

           }


This will remove the dynamically addded rows from the DOM but the $scope objects, you will have take care to meet the business needs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900