Variable definer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Variable definer

my problem is that i want it to be a verable that you can type into to change it in the website or what not, rather than having to change the code within. It should work as far as i'm aware, but it doesn't. Why not? (more text inside the code to explain what i mean. ) https://code.sololearn.com/WLFF7dSVkF98/?ref=app

11th Apr 2021, 4:38 AM
MeRrikK
MeRrikK - avatar
2 Answers
+ 2
Nothing updates as you type because you have no event listeners to respond to those input events. You have no event listeners at all. The following is much closer to what you want. Replace all your JS with this: document.addEventListener('DOMContentLoaded', function() { //this part here doesn't work. it never changes that output, neither as a prompt var binaryToText = document.getElementById("binarytoText"); var output = document.getElementById('output'); function binaryToWords(str) { if(str.match(/[10]{8}/g)){ var wordFromBinary = str.match(/([10]{8}|\s+)/g).map(function(fromBinary){ return String.fromCharCode(parseInt(fromBinary, 2) ); }).join(''); return wordFromBinary; } return 'No match found'; } function updateOutput() { output.innerText = binaryToWords(binaryToText.value); } updateOutput(); binaryToText.addEventListener('input', updateOutput); }); That assumes you add an element like this span with id="output" to your HTML. <textarea autofocus class="binarytotext" id="binarytoText">01100001 00100000 01100010</textarea> <span id="output"></span>
11th Apr 2021, 5:34 AM
Josh Greig
Josh Greig - avatar
+ 2
Your function binaryToWords works fine. I have only implemented a solution, which can shows its output. This can you see MeRrikK in this code: https://code.sololearn.com/WwpXnVf5F9ar/?ref=app
11th Apr 2021, 8:38 AM
JaScript
JaScript - avatar