简体   繁体   中英

not getting value in html from with javaScript

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) }

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM