morrie
morrie

Reputation: 21

Javascript decimal to hexadecimal

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/

Upvotes: 2

Views: 114

Answers (1)

Richard Hamilton
Richard Hamilton

Reputation: 26444

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/

Upvotes: 1

Related Questions