Pregunta: He escrito un código pero imprime valores solo una vez y quiero imprimir el valor línea por línea junto al valor anterior como una publicación, ¿de acuerdo?
var post = [] function getVal() { // creating varable val for select all input data. const val = document.querySelector('input').value; document.getElementById('input').value = ""; console.log(val); //store data in an array for print. post.push(val) //call for print the input value in span tag document.getElementById('print1').innerHTML = val console.log(post); } <input id="input" class="inp" type="text" name="textbox"> <button id="btn" class="postbutton" type="button" name="button" onclick="getVal()">Post</button> <span id="print1"></span>Reemplace la siguiente línea de función getVal()
document.getElementById('print1').innerHTML = valCon
document.getElementById('print1').innerHTML += val + "<br>" var post = [] function getVal() { // creating varable val for select all input data. const val = document.querySelector('input').value; document.getElementById('input').value = ""; console.log(val); //store data in an array for print. post.push(val) //call for print the input value in span tag document.getElementById('print1').innerHTML += val + "<br>" console.log(post); } <input id="input" class="inp" type="text" name="textbox"> <button id="btn" class="postbutton" type="button" name="button" onclick="getVal()">Post</button> <span id="print1"></span>Puede intentar usar join , solo ajuste una línea de código
reemplazar
document.getElementById('print1').innerHTML = valcon
document.getElementById('print1').innerHTML = post.join('<br/>'); var post = [] function getVal() { // creating varable val for select all input data. const val = document.querySelector('input').value; document.getElementById('input').value = ""; //store data in an array for print. post.push(val) //call for print the input value in span tag document.getElementById('print1').innerHTML = post.join('<br/>'); } <input id="input" class="inp" type="text" name="textbox"> <button id="btn" class="postbutton" type="button" name="button" onclick="getVal()">Post</button> <br/> <span id="print1"></span>