-2

The error ReferenceError: b is not defined at HTMLButtonElement.onclick (/:32:30)

 let s;
function b(){
      s = document.getElementById(k).innerHtml;
      animates();
}
        <form class="form">
         <label>How much do you want to donate</label>
          <input type="number" id="k">
    <button id="l" onclick="b()">Cal</button>
    
        </form>


   
Liam
  • 27,717
  • 28
  • 128
  • 190
Dlyze I
  • 71
  • 8
  • 1
    `innerHtml` doesn’t exist. Did you mean [`innerHTML`](//developer.mozilla.org/docs/Web/API/Element/innerHTML)? Inline event handlers like `onclick` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. `b` clearly is out of scope as `onclick` expects your references to be in global scope. – Sebastian Simon Jan 27 '22 at 10:30
  • 1
    `getElementById("k")` (you told JS to use the value of a non-existing variable `k`) –  Jan 27 '22 at 10:31
  • yeah but i will give it a value later in the form is there a way to do that – Dlyze I Jan 27 '22 at 10:32
  • Also, note that ``s do not have an `innerHTML`. You need `valueAsNumber` or `value` in this case. This is all explained in the [documentation](//developer.mozilla.org/docs/Web/HTML/Element/input). One has to wonder, which outdated and incomplete tutorial you learned from… – Sebastian Simon Jan 27 '22 at 10:35

1 Answers1

1

no need to do a form for this, also inline events are not preferred use eventListeners in js.

NOTE: I've put input's value as result, but you didn't say what you exactly want, so I think this will work with you anyway

document.getElementById('l').addEventListener('click', function() {
  document.getElementById('result').innerHTML =document.getElementById('k').value;
})
<label>How much do you want to donate</label>
<input type="number" id="k">
<button id="l">Cal</button>
<p id="result"></p>
Sarout
  • 821
  • 4
  • 25