11// script.js
22document . addEventListener ( 'DOMContentLoaded' , function ( ) {
33 const kilogramsInput = document . getElementById ( 'kilograms' ) ;
4+ const gramsInput = document . getElementById ( 'grams' ) ;
45 const poundsInput = document . getElementById ( 'pounds' ) ;
6+ const milligramsInput = document . getElementById ( 'milligrams' ) ;
57 const convertButton = document . getElementById ( 'convertButton' ) ;
68 const clearButton = document . getElementById ( 'clearButton' ) ;
79
8- convertButton . addEventListener ( 'click' , function ( ) {
9- const kilograms = parseFloat ( kilogramsInput . value ) ;
10- if ( ! isNaN ( kilograms ) ) {
11- const pounds = kilograms * 2.20462 ;
12- poundsInput . value = pounds . toFixed ( 2 ) ;
13- } else {
14- poundsInput . value = '' ;
10+ function clearAllInputsExcept ( elementToExclude ) {
11+ if ( elementToExclude !== kilogramsInput ) kilogramsInput . value = '' ;
12+ if ( elementToExclude !== gramsInput ) gramsInput . value = '' ;
13+ if ( elementToExclude !== poundsInput ) poundsInput . value = '' ;
14+ if ( elementToExclude !== milligramsInput ) milligramsInput . value = '' ;
15+ }
16+
17+ kilogramsInput . addEventListener ( 'input' , function ( ) {
18+ clearAllInputsExcept ( kilogramsInput ) ;
19+ const kg = parseFloat ( kilogramsInput . value ) ;
20+ if ( ! isNaN ( kg ) ) {
21+ gramsInput . value = ( kg * 1000 ) . toFixed ( 2 ) ;
22+ poundsInput . value = ( kg * 2.20462 ) . toFixed ( 2 ) ;
23+ milligramsInput . value = ( kg * 1000000 ) . toFixed ( 2 ) ;
24+ }
25+ } ) ;
26+
27+ gramsInput . addEventListener ( 'input' , function ( ) {
28+ clearAllInputsExcept ( gramsInput ) ;
29+ const g = parseFloat ( gramsInput . value ) ;
30+ if ( ! isNaN ( g ) ) {
31+ kilogramsInput . value = ( g / 1000 ) . toFixed ( 2 ) ;
32+ poundsInput . value = ( g * 0.00220462 ) . toFixed ( 2 ) ;
33+ milligramsInput . value = ( g * 1000 ) . toFixed ( 2 ) ;
1534 }
1635 } ) ;
1736
1837 poundsInput . addEventListener ( 'input' , function ( ) {
19- const pounds = parseFloat ( poundsInput . value ) ;
20- if ( ! isNaN ( pounds ) ) {
21- const kilograms = pounds / 2.20462 ;
22- kilogramsInput . value = kilograms . toFixed ( 2 ) ;
23- } else {
24- kilogramsInput . value = '' ;
38+ clearAllInputsExcept ( poundsInput ) ;
39+ const lbs = parseFloat ( poundsInput . value ) ;
40+ if ( ! isNaN ( lbs ) ) {
41+ kilogramsInput . value = ( lbs / 2.20462 ) . toFixed ( 2 ) ;
42+ gramsInput . value = ( lbs / 0.00220462 ) . toFixed ( 2 ) ;
43+ milligramsInput . value = ( lbs * 453592.37 ) . toFixed ( 2 ) ;
2544 }
2645 } ) ;
2746
28- kilogramsInput . addEventListener ( 'input' , function ( ) {
29- poundsInput . value = '' ; // Clear the other input when one is being typed into
47+ milligramsInput . addEventListener ( 'input' , function ( ) {
48+ clearAllInputsExcept ( milligramsInput ) ;
49+ const mg = parseFloat ( milligramsInput . value ) ;
50+ if ( ! isNaN ( mg ) ) {
51+ kilogramsInput . value = ( mg / 1000000 ) . toFixed ( 2 ) ;
52+ gramsInput . value = ( mg / 1000 ) . toFixed ( 2 ) ;
53+ poundsInput . value = ( mg / 453592.37 ) . toFixed ( 2 ) ;
54+ }
3055 } ) ;
3156
32- poundsInput . addEventListener ( 'input' , function ( ) {
33- kilogramsInput . value = '' ; // Clear the other input when one is being typed into
57+ convertButton . addEventListener ( 'click' , function ( ) {
58+ // The input event listeners already handle the conversion in real-time.
59+ // This button could be repurposed for something else or removed.
60+ alert ( "Conversions are now real-time as you type!" ) ;
3461 } ) ;
3562
3663 clearButton . addEventListener ( 'click' , function ( ) {
3764 kilogramsInput . value = '' ;
65+ gramsInput . value = '' ;
3866 poundsInput . value = '' ;
67+ milligramsInput . value = '' ;
3968 } ) ;
4069} ) ;
0 commit comments