Home > Enterprise >  onclick running when page loads
onclick running when page loads

Time:01-20

I have some code in javascript that I just want the object to change when I click on it, but it runs the code as if I have already clicked on the object as soon as the page loads

"use strict";

function changeText(obj){
    obj.innerHTML = "DON'T CLICK ME!";
}

function init(){
    var obj = document.getElementById("xPhoto");
    obj.onclick = changeText(obj);
}

window.onload = init;

CodePudding user response:

You are calling the changeText function in init so you are setting obj.innerHTML = "DON'T CLICK ME!" on init

Based on your description, you want to assign the chagneText to the onclick of obj, something like: obj.onclick = changeText

  •  Tags:  
  • Related