Hello Friends Today, through this tutorial, I will tell you How to Hex to Decimal Convert Using JavaScript Without Submit Button? You can create a simple Hex to Decimal converter using JavaScript with HTML without a submit button by using the `input` event to trigger the conversion as the user types. Here’s an example:
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hex to Decimal Converter</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } label { display: block; margin-bottom: 8px; } input { width: 100%; padding: 8px; margin-bottom: 16px; box-sizing: border-box; } p { font-weight: bold; color: #333; } </style> </head> <body> <h2>Hex to Decimal Converter</h2> <label for="hexInput">Enter Hexadecimal:</label> <input type="text" id="hexInput" placeholder="Enter hexadecimal"> <p id="decimalResult"></p> <script> // Function to convert hex to decimal function hexToDecimal(hex) { return parseInt(hex, 16); } // Get the input element const hexInput = document.getElementById('hexInput'); // Add an input event listener to trigger conversion as the user types hexInput.addEventListener('input', function() { const hexValue = hexInput.value.trim(); // Check if the input is a valid hex value if (/^[0-9A-Fa-f]+$/.test(hexValue)) { const decimalValue = hexToDecimal(hexValue.toUpperCase()); document.getElementById('decimalResult').textContent = `Decimal: ${decimalValue}`; } else { document.getElementById('decimalResult').textContent = 'Invalid Hexadecimal'; } }); </script> </body> </html>
This code includes an `input` event listener on the hex input field, which triggers the conversion function (`hexToDecimal`) as the user types. It checks if the input is a valid hex value before performing the conversion and updating the result on the page. The result is displayed in a paragraph with the id `decimalResult`.