-1

The simple javascript is not working. When I test the code in live preview (chrome), it says "ThfJ8q9:58 Uncaught ReferenceError: textpage is not defined at HTMLButtonElement.onclick (ThfJ8q9:58)"

What I am trying to do is to change the background image of the div "chat" when the button is clicked to the new image specified.

HTML:

<div id="chat"> 
    <div class="button-class">
     <button type="button" onclick="textpage()"> <img class= "submit-button- 
      img" alt="submit-button" src="images/text.button.png"> </button>
    </div>  
</div>

JAVASCRIPT DOCUMENT:

function textpage() {
document.getElementById("chat").style.backgroundImage = "url('full-convesation- 
MRP.png')";
}

1 Answers1

0

Could use something like this to listen for a click on the button

https://codepen.io/CTBroon/pen/RwbRGQq

HTML


    <div id="chat"> 
        <div class="button-class">
         <button type="button" id="btns"> <img class= "submit-button- 
          img" alt="submit-button" src="images/text.button.png"> </button>
        </div>  
    </div>

JS


    var btntrigger = document.getElementById('btns');

    btntrigger.addEventListener('click', function(){
      document.getElementById("chat").style.backgroundImage = "url('https://placehold.it/400x400')";
    })

Or have a closer look at your syntax on the orginal, fixed here:

https://codepen.io/CTBroon/pen/RwbRGQq

HTML


    <div id="chat"> 
        <div class="button-class">
         <button type="button" onclick="textpage()">
           <img class="submit-button-img" alt="submit-button" src="images/text.button.png"> </button>
        </div>  
    </div>

JS

function textpage() {
  document.getElementById("chat").style.backgroundImage = "url('https://placehold.it/400x400')";
}

:)

Connor Brown
  • 175
  • 6