Home > Net >  not getting value in html from with javaScript
not getting value in html from with javaScript

Time:10-14

I am not getting value in the alert box I am trying to get all values by using function with onclick. It gave me an empty value so how do I get all value with this keyword.

form

<form>
  <input type="text" id='textid' />
  <button class="btn" type="submit" onclick="myfunc(this);">click</button>
</form>

function

function myfunc(el){
  let x = el.value;
  alert(x) }

CodePudding user response:

You are alerting value of button, instead you should first get the input and then get its value using .value property

const input = document.querySelector("input");

function myfunc(el) {
  let x = input.value;
  alert(x)
}
<form>
  <input type="text" id='textid' />
  <button class="btn" type="submit" onclick="myfunc(this);">click</button>
</form>

  • Related