简体   繁体   中英

How take html dash-case input tag name in js

Case to naming all HTML parameters and now I have some troubles. I want take value from form input but JS don't taking dash-case. There is any way to take form input without changing input name ?

HTML and JavaScript:

 const form = document.querySelector('#message-form'); console.log(form.user-message.value); //Here rise error
 <form id="message-form"> <input type="text" name="user-message"> </form>

You can access it like this:

form['user-message'].value

Change name to usermessage and it will work

<form id="message-form">
    <input type="text" name="usermessage">
</form>


const form = document.querySelector('#message-form');
console.log(form.usermessage.value)

Or add querySelector for message

var message = form.querySelector('input[name="user-message"]').value;

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