How to code on java with conditions?

Hi experts, I have a java code;

function getStuff()
{
    document.querySelector('#tuna').onclick=talk;
}

function talk()
{
    alert('tuna');
}
window.onload=getStuff;

My problem is I want to add another 1 like document.querySelector('#bacon').onclick=talk; then I want the word bacon will show on alert message.
Any help pls?
Whing Dela CruzAsked:
Who is Participating?
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
There are a couple of ways that you could do it, but probably the easiest, is to just add the second onclick handler pointing to the same function, and then have that function display a different word depending on what element initiated the click event. Something like this...

function getStuff()
{
    document.querySelector('#tuna').onclick=talk;
    document.querySelector('#bacon').onclick=talk;
}

function talk(e)
{
    alert(e.target.id);
}
window.onload=getStuff;

Open in new window


Note: by the way, this is Javascript you are asking about not Java, they are two quite different things.
0
 
Whing Dela CruzAuthor Commented:
Thanks mccarl, its working!
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.