简体   繁体   中英

How to pass variable from an HTML file to a JavaScript file

I have two different files: a.html and a.js. In the html file I have a button, that when I click it I call a function located inside the.js file. In this moment, when I click the button, I want to pass a parameter that I receive from an on the html file to the js one. I've tried, from the js, to do Document.getElementById("idDelInput") but since the input is not inside the same file, it doesn't recognise the Document.

 function login() { //check the variable email }
 <input id="email" type="text"> <button onClick="login()">Login</button>

You need to grab the element first into your Javascript and then get the value from it. document.getElementById("email") , or document.querySelector("#email") will get your element into the javascript file. Then use value property to get its value.

 function login() { document.querySelector("#email").value; }
 <input id="email" type="text"> <button onClick="login()">Login</button>

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