Why is input not working correctly?

  • 0
    The essence of the error: for example, I write the number 100 in the input and select the parameter that I need from the select and press the button and the result of the action should be displayed in the console, but displays the following: & lt; input type = "number" id = "inp" & gt;

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <div class="Select" >
        <button>Выбрать</button>
        <select class = "select" >
            <option>Граммы в килограммы</option>
            <option>Килограммы в центлеры</option>
            <option>Центлеры в тонны</option>
        </select>
        <input  type="number" id = "inp">
        </div>
        
        <script src="./script.js"></script>
    </body>
    </html>

    let select = document.querySelector('.select');
    let number = document.getElementById('inp');
    
    document.querySelector('button').onclick =  () => {
        let spisok = select.value;
        //let number = prompt('Введите вес');
        switch (spisok) {
            
            case 'Граммы в килограммы': 
            
            let resultMassa1 = number / 1000;
            console.log(resultMassa1);
            break;
    
            case 'Килограммы в центлеры':
            
            let resultMassa2 = number / 100;
            console.log(resultMassa2);
            break;
    
            case 'Центлеры в тонны': 
            
            let resultMassa3 = number * 0.1;
            console.log(resultMassa3);
            break;
            
        }
    }
    JavaScript Anonymous, Aug 4, 2020

  • 2 Answers
  • 0
    In your variable number the input itself is written, not its value. Bugfix:

    let number = Number(document.getElementById('inp').value);
    Anonymous

  • 0
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!