Remove the image by clicking on the "X" icon located on the top right corner of the image

One of my tasks involves deleting an image by clicking on the "X" mark located at the top right corner of the image. To achieve this, I referred to this CSS fiddle http://jsfiddle.net/yHNEv/.

Sample HTML code:

<div class="img-wrap">
  <span ng-click="deleteLocalFile()" class="close">&times;</span>
  <a id="imageLink" href><img ng-src="{{imagePreviewUrl}}" style="width: 100px; height: 100px;" alt=""></a>
</div>

Corresponding Controller Code:

$scope.deleteLocalFile = function() {
  var element = document.getElementsByClassName("close");
  var wrappedElement = angular.element(element);
  
  wrappedElement.remove();
}

The above code successfully removes the "X" mark when clicked, but it does not delete the image itself. However, a click event is detected upon clicking the "X" mark.

An alternative method:

<div class="img-wrap">
  <span class="close">&times;</span>
  <a ng-click="deleteLocalFile()" id="imageLink" href><img ng-src="{{imagePreviewUrl}}" style="width: 100px; height: 100px;" alt=""></a>
</div>

Controller code for the second approach:

$scope.deleteLocalFile = function() {
  var element = document.getElementsById("imageLink");
  var wrappedElement = angular.element(element);
  
  wrappedElement.remove();
}

In this alternate approach, no click event is triggered when clicking the "X" mark, and instead, an event occurs when the image is clicked, leading to the deletion of only the image while keeping the "X" mark intact.

Answer №1

This version of the response is a rephrased explanation of Sasikumar's solution without using jQuery.

var closeBtns = document.querySelectorAll('.img-wrap .close')

for (var i = 0, l = closeBtns.length; i < l; i++) {
  closeBtns[i].addEventListener('click', function() {
    var imgWrap = this.parentElement;
    imgWrap.parentElement.removeChild(imgWrap);
  });
}
.img-wrap {
  position: relative;
  display: inline-block;
  border: 1px red solid;
  font-size: 0;
}
.img-wrap .close {
  position: absolute;
  top: 2px;
  right: 2px;
  z-index: 100;
  background-color: #FFF;
  padding: 5px 2px 2px;
  color: #000;
  font-weight: bold;
  cursor: pointer;
  opacity: .2;
  text-align: center;
  font-size: 22px;
  line-height: 10px;
  border-radius: 50%;
}
.img-wrap:hover .close {
  opacity: 1;
}
<div class="img-wrap">
  <span class="close">&times;</span>
  <img src="http://images.freeimages.com/images/premium/previews/2282/2282459-fisheye-tank.jpg" width="200" data-id="123">
</div>

<div class="img-wrap">
  <span class="close">&times;</span>
  <img src="http://images.freeimages.com/images/previews/38a/kendo-armor-3-1431999.jpg" width="100" data-id="103">
</div>

If you prefer JSFiddle, here is the link: https://jsfiddle.net/TheQueue841/0xugckje/

Answer №2

Give this code a shot

$('.img-wrap .close').on('click', function() {
    $(this).closest('.img-wrap').remove();
});
.img-wrap {
    position: relative;
    display: inline-block;
    border: 1px red solid;
    font-size: 0;
}
.img-wrap .close {
    position: absolute;
    top: 2px;
    right: 2px;
    z-index: 100;
    background-color: #FFF;
    padding: 5px 2px 2px;
    color: #000;
    font-weight: bold;
    cursor: pointer;
    opacity: .2;
    text-align: center;
    font-size: 22px;
    line-height: 10px;
    border-radius: 50%;
}
.img-wrap:hover .close {
    opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="img-wrap">
<span class="close">&times;</span>
<img src="http://images.freeimages.com/images/premium/previews/2282/2282459-fisheye-tank.jpg" width="200" data-id="123">
</div>

<div class="img-wrap">
<span class="close">&times;</span>
<img src="http://images.freeimages.com/images/previews/38a/kendo-armor-3-1431999.jpg" width="100" data-id="103">
</div>

Check out the jsfiddle demo here http://jsfiddle.net/yHNEv/356/

Answer №3

Modify the following CSS:

.container {
    position: relative;
    display: inline-block;
    border: 1px blue solid;
    font-size: 0;
}

.container .close-button {
    position: absolute;
    top: 0px;
    right: 0px;
    z-index: 100;
    cursor: pointer;
    opacity: 1;
    height: 100%;
    width: 100%;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Grab the webpage's URL by collecting the link from the src attribute of the script tag using XSLT

Can XSLT be used to extract a URL link specified within the src attribute of a script tag in an HTML file? Here is an example of the HTML file: <HTML> <BODY> <SCRIPT language="javascript" src="http://myspace.com" type="text/javascript"> ...

The act of transferring non-textual information into web-based applications

Is it possible for a user to copy and paste a selection of pixels from MSPaint into a browser-based app using JavaScript in current browsers? If not, will HTML5 make this possible in the future? Alternatively, could something like Flex or Silverlight be us ...

Unable to establish an external connection with the node

Currently, I am in the process of establishing a connection to a local server and running an app on port 8080 using node. Both node and apache are installed on my system. When Apache is active, I can access the server externally. However, when Node is runn ...

Learn how to implement data-binding in AngularJS using the ui-router and ControllerAs syntax, all without the

I have been following the guidelines laid out by John Papa, but I am facing difficulty in binding values from child controllers to parent controllers using ui-router, ControllerAs, and vm variables instead of $scope. In my attempts, I created two example ...

Achieving functionality with dropdown menus in jQuery

I am facing an issue with a dropdown menu that works perfectly in jsFiddle during testing, but does not function as expected when I run it on my testing server. jsFiddle: http://jsfiddle.net/cyberjo50/39bu8/2/ HTML <!doctype html> <html> < ...

Unable to retrieve the value of a textbox located within a gridview

In my grid view, I have a textbox nested inside a TemplateField. I want to retrieve the value of this textbox when a button is clicked: This is where I create the textbox: <ItemTemplate> <asp:TextBox runat="server" ID="txtEmail" Text=&a ...

Do flexible layouts require more effort and time to create and upkeep compared to fixed width layouts?

Are flexible layouts more challenging to create and maintain than fixed width layouts, and do they take longer to design? Does a flexible layout only involve setting the width, height, and font size in em or %? What are the main advantages of using a fle ...

Aligning an SVG icon at the center of a button

I have elements that are currently being used as buttons, but I want to switch them out for actual button tags. This would improve accessibility and allow keyboard navigation using the tab key to focus on my buttons. Each button contains a centered SVG ima ...

AngularJS: splitting the parent <div> into multiple sections every nth element

I have an array of strings in Javascript and I am attempting to use AngularJS to create nested <div> elements. var arr = ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu"]; My goal is to group every 3 elements together like so. <div class="pare ...

Ways to utilize CSS for capturing user-inputted text in a text field

I have a question about incorporating user input text into CSS styling. Specifically, I am looking to create a color background option (#ffffff) where the designer can input their own color and have it displayed once saved in the body background style. Wh ...

Tips for implementing border-radius on Internet Explorer 8:

Similar Question: How to create rounded borders in IE8 using CSS? I typically use css border-radius for the design of my webpages. While they display well in Firefox, I am facing compatibility issues with IE8. Can anyone offer any assistance? Thank yo ...

The initial row's Id will be used as the Id for all subsequent rows within a JSON object

After dragging a tr in a table and confirming by clicking a button, I need the order list to be updated in the database. To achieve this, I created a JSON object where I intend to save the ids and their corresponding new orders. The issue I am facing is t ...

Does the user need to download the scripts from Google API multiple times during the insertion process?

Concerned about the potential need for multiple downloads of a script from an unknown location on the user's computer, I would like to explore options to mitigate this issue. Specifically, I am considering creating a condition to check the download st ...

Struggling to delete items from an array in react.js

I am attempting to remove an item from a nested childArray within another Array. This is my current approach: const childArrayHandler = (childData, sub, questionId, data, btnId) => { // Manage color change on click const isInList = selectedBtnL ...

Axios encounters CORS issues, while fetch operates smoothly

After going through various questions on CORS errors to no avail, I am facing a dilemma in my NuxtJS client application. Whenever I try to make a simple POST request using axios, I encounter CORS issues. However, when I switch to using the fetch API, every ...

retrieve the complete webpage content, encompassing the .html file, images, .js files, css stylesheets, and more

I'm currently working on a project and could use some assistance. Is there a method available to download an entire page, including the .html file, images, .js files, css, etc., using PHP, JavaScript, or AJAX? <html> <body> & ...

Ways to verify if a web URL is contained within an HTML textbox

In the process of developing a frontend form for WordPress, I have incorporated a basic HTML textbox for users to input a web URL. My goal now is to ensure that the entered value is indeed a valid URL and not just arbitrary text. Is there a way to perfor ...

Assign an AJAX call to retrieve data from a MySQL table and store it in a

In my database, I have a table that includes fields for 'user_name' and 'user_round'. My goal is to set the 'user_round' field to match the value of a JavaScript variable called 'Level'. However, when I run my code, ...

Is there a way to incorporate the load page plugin specifically for JavaScript while ensuring that my PHP code is still functional?

Is there a way to implement the loading page plugin "PACE" for PHP code execution instead of just JavaScript? I am more comfortable with PHP, CSS, and HTML, but not very experienced in JavaScript/AJAX. The PACE plugin is designed to show a progress bar fo ...

Creating an adjustable fixed footer using Bootstrap 3

Struggling with the application of bootstrap styling. I have a footer with columns set as col-md-3, 3, 2, 4 which sums up to a total of 12. The issue lies in the differing content within each column. My goal is to achieve equal spacing between each div in ...