Conversion from Decimal to octal, binary and hexadecimal in javascript

前端 未结 5 1467
花落未央
花落未央 2021-02-08 17:23

I have a form where a user enters a decimal number and then from the dropdown menu he chooses if he wants to convert it either to binary, octal or hexadecimal by clicking on a c

5条回答
  •  不知归路
    2021-02-08 18:03

    I see some error

    it's not getElementbyId ==> is getElementById, bad use of the context(this) ... in your function Answer (this) is pointing to to the button .. I think you need to change the value on the input, right

    try this:

    function Answer(e) {
        e.preventDefault();
        var input = document.getElementById('input');
        if (document.getElementById('selectid').value ==="binary") {
            input.value = Number(input.value).toString(2);
        }
        else if  (document.getElementById('selectid').value ==="octal") {
            input.value = Number(input.value).toString(8);
        }
        else if  (document.getElementById('selectid').value ==="hexadecimal") {
            input.value = Number(input.value).toString(16);
        }
    }   
    

    Note: add id='input' to the input on the HTML part

提交回复
热议问题