I'm going into javascript right now. I have a question

Asked 2 years ago, Updated 2 years ago, 97 views

Hello, developers. I'm going to make a conditional statement and implement it.

if (b!==number) { alert('Number only allowed')} else {a*b}

a="What I typed in advance and am currently printing on the screen"

b= Like the code below, I designate it as the number value that the user enters in the text I made I want to, but how should I designate this?

<input type="text" name="user input value" value="number value entered by the user">

selector

2022-09-22 12:24

3 Answers

There are so many ways. Here's an example.

let inputValue = document.querySelector('input').value;
console.log(inputValue);

There are many ways and browser compatibility by method.


2022-09-22 12:24

It's really hard to know how to enter the value of b.


2022-09-22 12:24

var number = document.getElementById("number");
var result = document.getElementById("multiply");
var btn = document.getElementById("btn");
var numberValue = 0;
var preNumber = 10; // Pre-populated number

function multiply(){
    if (typeof numberValue !== "number") {
        alert('Only numbers are allowed.');
    } } else {
        var multiplyTwo = preNumber * numberValue;
    }
    result.innerHTML = multiplyTwo;
}

function readNumber(){
    numberValue = parseInt(number.value);
}

number.addEventListener("keypress", readNumber);
btn.addEventListener("click", multiply)

;

html file

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Page Title</title>

</head>
<body>
    <input type="text" name="user input value" id="number" value="> // value is not pre-populated.
    <button id="btn">Submit</button>
    <p id='multiply'></p>
    <script src="hashcode3.js"></script>
</body>
</html>


2022-09-22 12:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.