@@ -6,6 +6,7 @@ document.addEventListener('DOMContentLoaded', function() {
66 const milligramsInput = document . getElementById ( 'milligrams' ) ;
77 const convertButton = document . getElementById ( 'convertButton' ) ;
88 const clearButton = document . getElementById ( 'clearButton' ) ;
9+ const highlightDuration = 300 ; // milliseconds
910
1011 function saveConversion ( ) {
1112 localStorage . setItem ( 'lastKilograms' , kilogramsInput . value ) ;
@@ -25,7 +26,6 @@ document.addEventListener('DOMContentLoaded', function() {
2526 if ( lastPounds !== null ) poundsInput . value = lastPounds ;
2627 if ( lastMilligrams !== null ) milligramsInput . value = lastMilligrams ;
2728
28- // Trigger input events to perform conversions on load if values are present
2929 if ( lastKilograms ) kilogramsInput . dispatchEvent ( new Event ( 'input' ) ) ;
3030 else if ( lastGrams ) gramsInput . dispatchEvent ( new Event ( 'input' ) ) ;
3131 else if ( lastPounds ) poundsInput . dispatchEvent ( new Event ( 'input' ) ) ;
@@ -39,16 +39,26 @@ document.addEventListener('DOMContentLoaded', function() {
3939 if ( elementToExclude !== milligramsInput ) milligramsInput . value = '' ;
4040 }
4141
42+ function highlightInput ( inputElement ) {
43+ inputElement . classList . add ( 'conversion-highlight' ) ;
44+ setTimeout ( ( ) => {
45+ inputElement . classList . remove ( 'conversion-highlight' ) ;
46+ } , highlightDuration ) ;
47+ }
48+
4249 kilogramsInput . addEventListener ( 'input' , function ( ) {
4350 clearAllInputsExcept ( kilogramsInput ) ;
4451 const kg = parseFloat ( kilogramsInput . value ) ;
4552 if ( ! isNaN ( kg ) ) {
4653 gramsInput . value = ( kg * 1000 ) . toFixed ( 2 ) ;
4754 poundsInput . value = ( kg * 2.20462 ) . toFixed ( 2 ) ;
4855 milligramsInput . value = ( kg * 1000000 ) . toFixed ( 2 ) ;
56+ highlightInput ( gramsInput ) ;
57+ highlightInput ( poundsInput ) ;
58+ highlightInput ( milligramsInput ) ;
4959 saveConversion ( ) ;
5060 } else {
51- saveConversion ( ) ; // Still save even if input is cleared
61+ saveConversion ( ) ;
5262 }
5363 } ) ;
5464
@@ -59,6 +69,9 @@ document.addEventListener('DOMContentLoaded', function() {
5969 kilogramsInput . value = ( g / 1000 ) . toFixed ( 2 ) ;
6070 poundsInput . value = ( g * 0.00220462 ) . toFixed ( 2 ) ;
6171 milligramsInput . value = ( g * 1000 ) . toFixed ( 2 ) ;
72+ highlightInput ( kilogramsInput ) ;
73+ highlightInput ( poundsInput ) ;
74+ highlightInput ( milligramsInput ) ;
6275 saveConversion ( ) ;
6376 } else {
6477 saveConversion ( ) ;
@@ -72,6 +85,9 @@ document.addEventListener('DOMContentLoaded', function() {
7285 kilogramsInput . value = ( lbs / 2.20462 ) . toFixed ( 2 ) ;
7386 gramsInput . value = ( lbs / 0.00220462 ) . toFixed ( 2 ) ;
7487 milligramsInput . value = ( lbs * 453592.37 ) . toFixed ( 2 ) ;
88+ highlightInput ( kilogramsInput ) ;
89+ highlightInput ( gramsInput ) ;
90+ highlightInput ( milligramsInput ) ;
7591 saveConversion ( ) ;
7692 } else {
7793 saveConversion ( ) ;
@@ -85,6 +101,9 @@ document.addEventListener('DOMContentLoaded', function() {
85101 kilogramsInput . value = ( mg / 1000000 ) . toFixed ( 2 ) ;
86102 gramsInput . value = ( mg / 1000 ) . toFixed ( 2 ) ;
87103 poundsInput . value = ( mg / 453592.37 ) . toFixed ( 2 ) ;
104+ highlightInput ( kilogramsInput ) ;
105+ highlightInput ( gramsInput ) ;
106+ highlightInput ( poundsInput ) ;
88107 saveConversion ( ) ;
89108 } else {
90109 saveConversion ( ) ;
@@ -100,7 +119,7 @@ document.addEventListener('DOMContentLoaded', function() {
100119 gramsInput . value = '' ;
101120 poundsInput . value = '' ;
102121 milligramsInput . value = '' ;
103- saveConversion ( ) ; // Save empty values to clear local storage on clear
122+ saveConversion ( ) ;
104123 } ) ;
105124
106125 loadLastConversion ( ) ;
0 commit comments