How to Convert Octal to Binary Using JavaScript With HTML, CSS?

Hello Friends Today, through this tutorial, I will tell you How to octal to Binary Convert Using JavaScript Without Submit Button with HTML? You can create an Octal to Binary converter using JavaScript and HTML without a submit button by updating the binary output dynamically as the user types the octal input. Here’s a simple 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>Octal to Binary Converter</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
label {
display: block;
margin-bottom: 8px;
}
input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
</style>
</head>
<body>
<h2>Octal to Binary Converter</h2>
<label for="octalInput">Enter Octal Number:</label>
<input type="text" id="octalInput" placeholder="Enter octal number" oninput="convertOctalToBinary()">
<p id="binaryOutput"></p>
<script>
function convertOctalToBinary() {
const octalInput = document.getElementById('octalInput').value;
const binaryOutput = document.getElementById('binaryOutput');
// Validate input: check if the input is a valid octal number
if (/^[0-7]+$/.test(octalInput)) {
const binaryResult = parseInt(octalInput, 8).toString(2);
binaryOutput.textContent = `Binary: ${binaryResult}`;
} else {
binaryOutput.textContent = 'Invalid octal input';
}
}
</script>
</body>
</html>

In this example, the `convertOctalToBinary` function is triggered on every input event in the octal input field. The function checks if the input is a valid octal number using a regular expression. If it is valid, it converts the octal input to binary using the `parseInt` function with base 8 and then `toString(2)`. The result is then displayed in the `binaryOutput` paragraph. If the input is not a valid octal number, an error message is displayed.