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); }
xhr.responseTextwithout quotes. And transfer the form data toxhras follows: var firm ='firm=' + encodeURIComponent(e.value). - Artem Gorlachevreq.body.firm+body-parser; without your server code I can’t tell you more precisely - Artem Gorlachevapp.post('/...',function());- ItsMyLifeapp.post('/...',function(req, res){ console.log(req.body.firm) }), but for you to have req.body you need to connectbody-parser- Artem Gorlachev