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

Difficulty establishing a connection between data in app.js and index.html using Ionic framework

Struggling with connecting index.html to get data for my Ionic mobile app. Any tips or guidance would be highly appreciated as I'm new to this! The goal is to display a list of restaurants in the app, utilizing Cards Images to showcase an image, logo ...

Adjusting the background color of an individual element within a grid using CSS

<div id="56c46f8385953" class="mg_box mg_pre_show col1_3 row1_3 m_col1_2 m_row1_3 mgi_14 mg_pag_1 mg_gallery mg_transitions mg_closed " rel="pid_14" mgi_w="0.333" mgi_h="0.333" mgi_mw="0.5" mgi_mh="0.333" > <div class="mg_shadow_div"> ...

Detecting Android devices

Issue: My website functions properly on desktop but has a problem with the carousel images skewing on iPhones. To address this, I created a separate CSS styling for iPhone devices and used a JavaScript function found online to detect iPhones and iPads. Un ...

The note-taking app's JavaScript code does not currently have the capability to store notes in local storage

const noteContainer = document.querySelector(".note-container"); const createBtn = document.querySelector(".btn"); let notes = document.querySelectorAll(".input-box"); function showNotes() { noteContainer.innerHTML = localS ...

Change the identifier of a value within the React state

I am currently working on a form that includes input fields for both keys and values. The goal is to allow users to edit key value pairs, where editing the value field is straightforward, but editing the key field requires updating, removing, and tracking ...

What are some techniques for reducing the number of pages in my Carousel?

I need help figuring out how to set a limit of noOfPages for the number of pages per carousel. The functions in my code below don't implement this and I'm unsure how to do it. const itemsPerPage = 6; const [page, setPage] = React.useState(1); ...

Limiting the size of images within a specific section using CSS

When using CSS, I know how to resize images with the code snippets below: img {width:100%; height: auto; } img {max-width: 600px} While this method works effectively, it applies to every image on the page. What I really need is for some images to be ...

Next.js is causing an error by not recognizing the document variable

While diving into the world of next.js, I encountered an interesting challenge. In my project, I came across this puzzling error. The culprit seemed to be a module called Typed.js, which threw me off with a peculiar message: Server Error ReferenceError: d ...

Is there a way to use PHP in place of the <form> tag in HTML in order to submit a form from a script and remain on

Currently, the code in my form looks like this: <form action="https://www.paypal.com/cgi-bin/webscr" target="_BLANK" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="paypal@ ...

"Encountering a 404 error while trying to refresh

I've already attempted to include <base href="/index.html"> and/or <base href="/"> My .htaccess configuration is as follows: Options +FollowSymLinks <ifModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME ...

Is there a way to add Internet Explorer specific stylesheets to the Wordpress functions directory using enqueue?

I'm facing challenges setting up my Wordpress theme to be compatible with early versions of Internet Explorer. Most online tutorials have advised me to enqueue specific I.E stylesheets in the header, but this method has not been successful for me. Add ...

Techniques for transferring checkbox values to a JavaScript function

Here is the code snippet I am working with: <input type="checkbox" name="area" id="area" value="0">Some Text1</input> <input type="checkbox" name="area" id="area" value="80">Some Text2</input> Additionally, here is the JavaScript ...

Disable the "top" property for the Material UI Drawer component

I'm currently working on implementing a Persist Left Drawer from Material UI that slides in from the left side. However, I don't want the drawer to cover the entire left side of the page. Essentially, I want to remove the "top" style property men ...

Can you explain the concept of fallback color in CSS?

Can you explain to me what a fallback color means? I searched online and found out that it is used to display a color when the specified format is not supported by browsers, like Internet Explorer. Is there anything else I should know about fallback colors ...

Execute my function when the Redux state changes in the store in ReactJS

I am facing a specific issue in my React-Redux application. I am using Redux-saga to communicate with a REST API, which does not return a promise. Within my application, I also utilize state (in addition to the store) that I wish to update after receiving ...

Adjust the height of each card dynamically based on the tallest card in the row

I am working on a row that looks like this: <div class="row"> <div class="col"> <div class="card"> <div class="card-body"> <h3 class="card-title ...

Utilize the dynamic backlash/Werkzeug debugger during an unsuccessful AJAX request on a TurboGears server

TurboGears offers the unique feature of "backlash," an interactive debugger within the browser that is built on the Werkzeug Debugger. When server-side debugging is enabled, in case of a request failure, an interactive web page is displayed where users can ...

What is the reason for the excessive width of the final column in this table?

I am currently working with a dataset that I am displaying using the handsontable library in the form of a table. One issue I am facing is that the last column of my table appears too wide even though I did not specify any width for it. Below you can see t ...

Extracting JSON Data into Text Boxes within jQuery UI Dialogs

Let me provide some context to my current issue. Due to the nature of the problem being work-related, I am unable to share much of the code. The task at hand involves working with a jQuery dialog. Essentially, I have a list of names displayed with a boots ...

Change the size of a custom cursor on click using React

I have customized the cursor on my react app, but I want to make it animate when the user clicks. Perhaps by decreasing its size or some other effect. The cursor is within a component that I've included in my Index.js file. I am unsure how to create a ...