Good day. I have a drop down menu. Is it possible to send the values ​​of this field to the node js server? Without submitting the form. Just when choosing a list item? HTML code

<form> <input type="text" name="change" id="changing-input" list="name_list"> <datalist id="name_list"> <option id='key' value='key' + onclick="selectPhone(this)"></option> </datalist> <input type="text" name="" id="changing-input"> <input type="button" name="" value="Change Phone"> </form> 

and an exemplary Ajax request (as I imagine, but not sure what is most likely possible)

 function selectPhone(e) { console.log(e.value); var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { if(!xhr.responseText) { document.getElementById('anotherFirm').innerHTML = 'Take another firm'; }else{ document.getElementById('changing-input').value = "xhr.responseText"; } }else if (xhr.readyState == 4 && xhr.status != 200) { console.dir('error'); } }; // xhr.open('POST', '/registration', true); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8"); var firm = e.value; xhr.send(firm); } 
  • one
    Yes, this is how it should work, only xhr.responseText without quotes. And transfer the form data to xhr as follows: var firm = 'firm=' + encodeURIComponent(e.value) . - Artem Gorlachev
  • @ArtemGorlachev and how then to accept it on the server? - ItsMyLife
  • req.body.firm + body-parser ; without your server code I can’t tell you more precisely - Artem Gorlachev
  • @ArtemGorlachev I didn’t say a bit how to process this particular request; when I send a form, I process app.post('/...',function()); - ItsMyLife
  • well, everything is correct - app.post('/...',function(req, res){ console.log(req.body.firm) }) , but for you to have req.body you need to connect body-parser - Artem Gorlachev

0