var image1=new Image(); // добавление картинки image1.src="3/2.jpg"; // расположение начального фона function q1(){ // начальный фон ctx.drawImage(image1,0,0); // прогрузка начального фона в координатах 0 0 } image1.onload=q1; // когда картинки загрузится //Когда нажимаю на кнопку картинка должна удалятся btn2=document.createElement( 'BUTTON'); // создание кнопки btn2.id='cmd2'; // её id btn2.textContent ='удалить картинку'; // её текст document.body.appendChild(btn2); // нужно чтобы добавилось btn2.onclick=function(){ // функция при клике на неё } 

Closed due to the fact that the essence of the question is not clear to the participants: Igor , Air , 0xdb , aleksandr barakin , Kromster January 21 at 4:34 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    Solution of your problem:

      var src = 'https://www.gravatar.com/avatar/4deff273cbbbb0e3a73938e37697036f?s=32&d=identicon&r=PG&f=1', id = 'myImage'; function createButtons() { // First button / Generate image var buttonGenerate = document.createElement('BUTTON'), buttonDelete = document.createElement('BUTTON'); buttonGenerate.textContent = 'Add'; buttonDelete.textContent = 'Delete'; var body = document.querySelector('body'); body.appendChild(buttonGenerate); body.appendChild(buttonDelete); buttonGenerate.addEventListener('click', createImgElement); buttonDelete.addEventListener('click', deleteImgElemet); } function createImgElement() { var image = document.createElement("IMG"); image.id = id; image.src = src; // image.alt = alt; document.querySelector('body').appendChild(image); } function deleteImgElemet() { var image = document.getElementById(id); if (image != undefined) { image.remove(); } else { console.log('Image is undefined'); } } createButtons(); 
     <body> <h1>Hello!</h1> </body> 

    Only the picture does not load, try to insert the code into your local development environment, everything should work there like a clock.

    • When I press the button, the picture should be deleted, added a question - Sergey Alexandrov
    • Let's see. You need to add a button that will remove the img element? Or do you need to create two buttons, when you click on one button, img will be created, and when you click on another, img will be deleted? - Dmitry
    • Two buttons, when you click on one button, img will be created, and when you click on another, img will be deleted. This is how it is - Sergey Alexandrov
    • @ SergeyAleksandrov I understood you, one minute ... - Dmitry
    • one
      Then correct the answer, if you understand the author's question, or else you clap the minuses ... I removed my own ... But there is no alarm, in case there is no answer, the admin will delete the answer - Air