2

I'm very new to programming and am trying to write an application for a decimal to hexadecimal converter. When you type a number into a number box (in base 10) and you see the base-16 equivalent appearing as the value in the box change. I cannot figure out how to make my program run. Any suggestions would be greatly appreciated! HTML:

Base Ten:
<input type="number" id="base10" onkeydown="convertBase10to16()">
<br>Base Sixteen:
<input id='base16'>

Javascript:

var base10 = 
    document.getElementById("base10");

var base16 = 
    document.getElementById("base16");

var convertBase10to16() = {function () {
    if (id("base10").value !== '') 
    {id("base16").value = parseInt(id("base10").value,10).toString(16);}
};

https://jsfiddle.net/8yvjyy4b/3/

2
  • 1
    Your code has several syntax errors, and uses a function called id without defining it. You should check the errors in your JavaScript console to try to solve your problems first; see How to open the JavaScript console in different browsers? Commented Oct 7, 2015 at 17:49
  • There are too many issues here to address. Please narrow down the scope of your question from asking us to complete it to explaining the exact place you got stuck. Commented Oct 7, 2015 at 17:51

1 Answer 1

1

Your code have several syntax errors. Here is what it should look like

var base10 = document.getElementById("base10");

var base16 =  document.getElementById("base16");

base10.addEventListener("blur", function() {
    var num = Number(this.value);
    base16.value = num.toString(16);
});

https://jsfiddle.net/8yvjyy4b/4/

Sign up to request clarification or add additional context in comments.

1 Comment

I disagree with the use of Number(n) instead of parseInt(n, 10). The former won't restrict the input to integers. It's one of the few things that the OP actually did correctly, although neither of you have any error detection.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.