Good day

I am trying to get a total of two numbers.
The first number I need to get form my page (the number changes every few seconds (crypto clicker)
Second number must be 35
This is the code I got after search, but do not know how to get itto work

            `<form>
            USD:<input type="text" name="num1"><br>
            Doge:<input type="text" name="num2"><br>
            Sum: <input type="text" name="sum"><br>
            <input type="button" value="Sum" onclick="calcSum()">
            </form>
         <script>
           function calcSum() {
        let num1 = document.getElementsByName("num1")[0].value;
        let num2 = document.getElementsByName("num2")[0].value;
        let sum = Number(num1) * Number(num2);
        document.getElementsByName("sum")[0].value = sum;
    }
</script>`

It's unclear what doesn't work. Just to check something out I tried some multiplication without Number() and it seems optional.
Can you explain:

  1. What doesn't work. Your code looks to be a fragment so I can't try it in an online Javascript tool.
  2. Your tags have Java yet I think this is Javascript.
  3. Why is Number() used.

Ok

Let me try again

  1. I want to multiply 2 numbers and give me the answer
  2. The first number will most times be the same (no need to input it)
  3. The second number I wiil input will be diffrent every time
  4. must multiply 2 2 numbers for output.

Number 1 input, most times will be 35
Number 2 will always change eg 200 or any number
output = 7000

  1. Prior to my reply I used an online Javascript system to double check I could multiple with and without using Number(). Both appear to give me the same result (the result of the multiple.) Can you give a solid example where you are having difficulty with this multiplication? Just one line for me to pop into Javascript please.
  2. No issue here?
  3. Seems to be no issue yet.
  4. Let's try some examples for me to put up online. There may be a typo here as to "2 2 numbers".

Thank you, will wait for your info

To be clear I'm waiting for you to supply what is called a minimum viable example of the issue you are having.

With you supplying me one line such as x = y * x; I can put that into an online javascript (not java unless you really meant that) test site so we can discuss this further. Maybe the problem is not about multiplication but something else?

Here is my code

<main>
<label for="firstNum">USD:</label>
<input type="number" id="firstNum" name="firstNum">

<label for="secondNum">Doge:</label>
<input type="number" id="secondNum" name="secondNum"></br></br>

<button onclick="multiply()">Multiply</button></br></br>

<label for="result">Total</label>
<input type="number" id="result" name="result"/>
</main>

<script>
function multiply(){
    num1 = document.getElementById("firstNum").value;
    num2 = document.getElementById("secondNum").value;
    result = num1 * num2;
    document.getElementById("result").value = result;
}
</script>

see screenshot of what I am trying to get to work.
I need doge to show 35 when i open the page

Screenshot_14.jpg

Thanks for the fairly complete code, but it appears your problem statement has changed.

I take it your new problem is: "I need doge to show 35 when i open the page"

If you want the Doge item to show 35 when it opens, then here's some thoughts.

  1. Line 7 might be:
    document.getElementById("secondnum").value = 35;
  2. Or line 6 would set the value. Maybe from https://www.w3schools.com/tags/att_input_value.asp or
    <input type="number" id="secondNum" name="secondNum" value=35></br></br>

Thank you, i did have it working befror, but it did not show the 35 in the box, and I did think it is not working, but now I have press the multiply button and it gives me the answer.

Thanks for helping me with this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.