Escribí un código para poder autocompletar una página web, el problema es que quiero que se autollenen 2 campos de texto, pero mi código solo llena el primero. Mi conocimiento es muy limitado, por lo que no sé cómo autocompletar el segundo campo de texto con una frase diferente.
let question = document.querySelector('crowd-form tr'); if (question) { let text = question.textContent.trim(); let input = question.querySelector('input'); if (input) { // Does text contain "man united"? if (text.includes('man united')) input.value = 'Manchester United FC'; // Does text contain "manchester united"? else if (text.includes('manchester united')) input.value = 'Manchester United FC'; } }Creé debajo del fragmento. ¿Puede echar un vistazo y comprobar si funciona según sus requisitos?
// Instead of using querySelector() I directly added the text just for an example. let text = 'man united is too shit for ronaldo'; // Reference of first textbox. let firstInput = document.getElementById('firstTextbox'); // Reference of second textbox. let secondInput = document.getElementById('secondTextbox'); // Assigning the value in firstTextbox based on the question text. if (firstInput) { firstInput.value = text.includes('man united') ? 'Man United FC' : text.includes('manchester united') ? 'Manchester United FC' : ''; } // Assigning the value in secondTextbox. if (secondInput) { secondInput.value = 'Old Trafford'; } <input id="firstTextbox" type="text"/> <input id="secondTextbox" type="text"/>