Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

onclick running when page loads

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;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading