I am trying to display in the console a string that is going to be passed to me through a .txt with .json format to load it in html.
Well specifically, I have an image that is going to have a title below it that is going to be in a label <h3>
but internally, how can the value ( .value()
) of the title change, I have created a function to which is passed as an argument the string from file. What I don't understand is how to use the id that I give the label <h3>
to make use of the created function.
This is what I have momentarily in the javascript:
function imprime(aux) {
console.log(aux);
}
document.getElementById('ttlSECT1_1').onclick = imprime('hola');
I would like to know how the function can be used when manipulating the javascript and before passing the object from .json, since later I may add more operations to the function in case it is needed.
Any advice?
What you do with
onclick
is execute a function after pressing an element, you can use whatever function you need to give it.You can save the element in a variable and give it all the functionality you want.
The way to use the GlobalEventHandler
.onclick
, as withaddEventListener
(which is the recommended way), is to pass the name of a function or the anonymous function, but to call a function with parameters it has to be done inside an anonymous function:Considering the code you have, the response would be like this:
So, when you click on the Label, it will show you in the console.