0

I am having the HTML as shown below. I want to delete the input box and also the image button itself when clicked.

<div id="missioncontainer" class="missioncontent">
    <div id="mission-{{mission.id}}" class="contentBox box effectmission" ng-repeat="mission in missions">
        <div class="boxheader">
            <div style="float:right; clear:right; width:20%;">
                <span><a href="" style="" ng-click="deleteMission(mission.id)"><img src="assets/img/delete.png" style="width: 20%; height: 20%;"></a></span>
            </div>
            <span style="font-size: large; font-family: monospace; font-weight: bold;">EDIT MISSION NAME</span>
            <input ng-blur="updatemission(mission.id, mission.missionInfo)" type="text" ng-model="mission.missionInfo" class="form-control" style="background-color: #e8e8e8">
        </div>
        <div style="padding-top: 10px;">
            <span style="font-size: large; font-family: monospace; font-weight: bold; margin-left:5%;">EDIT MISSION POINTS</span>
            <br />
        </div>
        <div style="padding-top: 10px;">
            <ul id="beforeul" style="float: left; width: 100%;">
                <li ng-repeat="missioncontent in mission.missioncontent" style="padding: 2px; width: 100%;">
                    <div style="float:right; clear:right; width:20%;">
                        <span><a href="" style="" ng-click="deletemissionpoints(missioncontent.id, mission.id)"><img src="assets/img/delete.png" style="width: 20%; height: 20%;"></a></span>
                    </div>
                    <div>
                        <input id="{{missioncontent.id}}" type="text" ng-model="missioncontent.info" ng-blur="updatemissionpoints(missioncontent.id, missioncontent.info)" class="form-control" style="background-color: #e8e8e8; width: 80%;">
                    </div>
                </li>
            </ul>
        </div>
        <div style="padding-top: 35px;">
            <a href="" ng-click="addmissionpoint(mission.id)"><img id="addmissionpoint" src="assets/img/add.png" alt="addmission" style="width: 7%;height: 12%;float: right; bottom: 0;"></a>
        </div>
    </div>
</div>

In the "ul" tag, the image and the input box also. I am writing Jquery as shown below, but only input box is getting deleted the image still remains. I need help on selecting the DOM elements using jQuery and calling remove() method on them.

$scope.deletemissionpoints = function(missionpointid, missionid){
    alert(missionpointid +" "+ missionid);
    jsonData.deletePointId.push(missionpointid);
       $("#mission-"+missionid+"").find("li #"+missionpointid+"").remove();
    $("#mission-"+missionid+"").find("#"+missionpointid+"").find("img").remove(); 
};
3
  • the DOM property outerHTML might be useful to you. You will have to retrieve the DOM element from Jquery element first. If you are getting an object undefined error then Tushar's answer below will help Commented Jun 24, 2015 at 6:47
  • 1
    You don't need ready in the function, remove that Commented Jun 24, 2015 at 6:52
  • 1
    yes, since it is in the function, i dont need ready Commented Jun 24, 2015 at 6:53

3 Answers 3

1

You can remove the element from the array mission.missioncontent in scope, which will also update the view using splice method.

See changes highlighted in the code:

HTML

<div style="padding-top: 10px;">
    <ul id="beforeul" style="float: left; width: 100%;">
        <li ng-repeat="missioncontent in mission.missioncontent" style="padding: 2px; width: 100%;">
            <div style="float:right; clear:right; width:20%;">
                <span><a href="" style="" ng-click="deletemissionpoints($index)">
                //                                                      ^^^^^^
                    <img src="assets/img/delete.png" style="width: 20%; height: 20%;"></a>
                </span>
            </div>
            <div>
                <input id="{{missioncontent.id}}" type="text" ng-model="missioncontent.info" ng-blur="updatemissionpoints(missioncontent.id, missioncontent.info)" class="form-control" style="background-color: #e8e8e8; width: 80%;">
            </div>
        </li>
    </ul>
</div>

Javascript

$scope.deletemissionpoints = function(index) {
    jsonData.deletePointId.push(missionpointid);

    $scope.mission.missioncontent.splice(index, 1);
    // Delete from the array
};
Sign up to request clarification or add additional context in comments.

2 Comments

@VikramSinghJadon Please share the complete javascript code
i am calling a function using ng-click when the image is clicked, so i think the DOM is loaded. i have added the full function of JS
0

You need to remove document.ready, this is an event handler for the DOM that is fired when the DOM has finished loading, in you method below the DOM has already loaded so the event will not execute.

Try this

$scope.deletemissionpoints = function(missionpointid, missionid){
    alert(missionpointid +" "+ missionid);
    jsonData.deletePointId.push(missionpointid);

   $("#mission-"+missionid+"").find("li #"+missionpointid+"").remove();
   $("#mission-"+missionid+"").find("#"+missionpointid+"").find("img").remove(); 
};

Comments

0

As answered by @Tushar, you need to wrap your code Into DOM ready event.

You can try this out!

$(document).ready(function() {
    $("#mission-"+missionid+"").find("li #"+missionpointid+"").remove();

    $("#mission-"+missionid+"").find("#"+missionpointid+"").find("img").remove();
});

See My jsFiddle - http://jsfiddle.net/ks6mo5ex/

or try this Out - http://jsfiddle.net/ks6mo5ex/1

3 Comments

see my jsfiddle.. I think this is what you want!
This is not what OP wants.. check the question again.
Bad times! He can then just append a dummy class and delete elements by classNames.. Can try updated fiddle - jsfiddle.net/ks6mo5ex/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.