1

i want to delete an image from s simple HTML page, when the delete button is pressed. I hav tried following code but it is not working, help please.

<script>
 function del()
 {
 var Node1 = document.getElementById(“i”);
 Node1.removeChild(Node1.childNodes[0]);
 alert("Image deleted");
 }
 </script>

<img src="file:///C|/Users/momo/Desktop/uf.PNG" id="i"/>
<button onclick="del()">Delete Image</button>
3
  • 1
    You need to use real quotes too Commented Apr 13, 2013 at 9:59
  • where should i use them? Commented Apr 13, 2013 at 19:20
  • Here. I do not know how you got curly quotes (“i”); Commented Apr 13, 2013 at 21:04

1 Answer 1

1

in your code:

 var Node1 = document.getElementById(“i”);
 Node1.removeChild(Node1.childNodes[0]);

your Node1 variable has no child nodes so it will never work, you need to use that on the parent node... But I wonder, wouldn't hide it fit your problem?

document.getElementById('i').style.display = 'none';

if not, you need to know what is the parent node of that image and then use removeChild correctly.

Sign up to request clarification or add additional context in comments.

Comments

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.