Check out, which Button is clicked


(Georg Isert) #1

I have 3 buttons with 3 different IDs. Now i want to check out, which button is clicked from a user. I write a external Javascript-File with a function to find out, which button ist clicked.
But it doesn’t work. If i click on a button, only the alert for button 1 ist coming up.
I’am a Java Beginner - what’s wrong?

Here my external Button-Check-Out-Script:

function cmd_Change(){
var cmdButton1 = document.getElementById('cmd_Button1');
cmdButton1.addEventListener('click', Iam(1));

var cmdButton3 = document.getElementById('cmd_Button2');
cmdButton2.addEventListener('click', Iam(2));

var cmdButton3 = document.getElementById('cmd_Button3');
cmdButton3.addEventListener('click', Iam(3));
}

function Iam(tempNumb){
  alert("I'am: " + tempNumb);
}

And here are the project-files:
3Buttons.zip (21.0 KB)

Thank you all for a answer.


#2

At the top of every custom hype JavaScript function, the following comments are added…

// element - DOMHTMLElement that triggered this function being called
// event - event that triggered this function being called

So, if you use “element.id”, you can get the name of the element that was clicked.


(Georg Isert) #3

Yes, that’s it.
Thank you very much. Now, for my future project, i can scripting all my Buttons in external javascripts for a better overview of my project. that is so much helpfull for me, because if you have all javascript functions for all buttons in the hype ressource folder, it`s not so easy to keep overview :smile:

A great idea for the developer would be a the possibility of organizing the files in ressource folder in a file structure similarly as in a file manager :slight_smile: :slight_smile: :slight_smile:

For all interested here are the finished example :smile:

3Buttons.zip (21.1 KB)


#4

I would have to disagree on this one. It’s much easier if you have the list of functions in Hype which you can sort according to type (drop down menu) and name and even size. It’s easier to keep track of the functions rather than have to trawl through a separate file. But, this is my opinion and I admit that others may have a different way. Whichever you find works best for you. :wink:

Just an FYI as you are a “Java” beginner. It’s Javascript not Java. Java is a separate language :smile: