I want to make a text to binary converter. For example if someone types in 'Hello' it would return 0100100001100101011011000110110001101111 How can I do this? I have tried this but something is wrong:
function convertBinary() {
var output = document.getElementById("outputBinary");
var input = document.getElementById("inputBinary").value;
output.value = "";
for (i=0; i < input.length; i++) {
output.value +=input[i].charCodeAt(0).toString(2);
}
}
Here is my code:
<!doctype html>
<html>
<head>
<title>Binary Converter</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function convertBinary() {
var output = document.getElementById("outputBinary");
var input = document.getElementById("inputBinary").value;
output.value = "";
for (i=0; i < input.length; i++) {
output.value +=input[i].charCodeAt(0).toString(2);
}
}
</script>
</head>
<body>
<center>
<div class="container">
<span class="main">Binary Converter</span><br>
<input type="text" onKeyUp="convertBinary()" id="inputBinary" class="inputBinary" placeholder="Enter Text"><br>
<input type="text" id="outputBinary" class="outputBinary" readonly>
</div>
</center>
</body>
</html>
Any help will be greatly appreciated. Thanks, Omar.