Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Is it possible to run an onclick javascript function inside another javascript function?

Currently learning javascript and was curious about this possibility. I have this code inside a .JS file

var x = document.createElement("IMG");
                    x.setAttribute("src", "helpimage.png");
                    x.setAttribute("width", "20");
                    x.setAttribute("height", "20");
                      x.setAttribute("alt", "help image");
                      x.setAttribute("id","image");
                    var row = document.createElement('div');
                    row.classList.add('row');
                    var label = document.createElement('div');
                        label.id = machineName + '-label';
                        label.classList.add('col-md-5');
                        label.innerHTML = humanName + ':';
                        label.appendChild(x);
                        label.onclick = function() {myFunction()};
                        
                    row.appendChild(label); 

and this function titled "MyFunction"

function myFunction() {
    document.getElementById(machineName + '-label').innerHTML = "YOU CLICKED ME!";
  }

What this code does is that first a function is called which creates a simple form element with a label. I attach a picture labeled "x" into the label. I would like to be able to click that label, with an onclick event, and then change it to whatever I want. (just testing here)

So once my first function generates my form, I then want to click my label and have it change to "YOU CLICKED ME".

Except when I click the picture nothing happens.

Can anyone tell me what I'm doing wrong?

picture of what I want to click. enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Put the text that you want to replace in another nested element, rather than in the element that also contains the image.

var x = document.createElement("IMG");
x.setAttribute("src", "helpimage.png");
x.setAttribute("width", "20");
x.setAttribute("height", "20");
x.setAttribute("alt", "help image");
x.setAttribute("id", "image");
var row = document.createElement('div');
row.classList.add('row');
var label = document.createElement('div');
let machineName = "filename";
label.id = machineName + '-label';
label.classList.add('col-md-5');
let humanName = "File Name";
let text = document.createElement("span");
text.id = machineName + "-text";
text.innerHTML = humanName + ':';
label.appendChild(text);
label.appendChild(x);
label.onclick = function() {
  myFunction()
};

row.appendChild(label);
document.body.appendChild(row);

function myFunction() {
  document.getElementById(machineName + '-text').innerHTML = "YOU CLICKED ME!";
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!