Skip to content
Advertisement

Buttons that change units of measurement dynamically on page

I am trying to implement a JavaScript function that will modify the result values that are outputted into a field.

Examples of results output to a field are kilobytes , and British pounds.

However I have included buttons in my form which will I will want to use to modify the results to make them in

Gigabytes, Megabytes and Pennies

See the screenshot below:

enter image description here

Pseudo Code:

JavaScript

The way I am thinking of implementing it will be I will have two Global variables in my JavaScript which will be changed depending on what button is pressed. Then they will be used in the calculations for data and money. Why a global? – well I have separate JavaScript functions that retrieve values and calculate them.

I need input on how to detect which button is pressed?

Also if you know of any similar examples of JS that would be great.

Any advice welcome!

Advertisement

Answer

In order to know which button is pressed add an onclick event handler to all the buttons and onclick event handler function you can detect the value of each of the button, from which you can identify the button clicked. For example

JavaScript

Hope this helps you.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement