5
I have a simple Javascript code to add a string to a list ul
:
JS:
function init() {
var botao = document.getElementById("addButton");
botao.onclick = handleButtonClick;
}
function handleButtonClick() {
var textInput = document.getElementById("songTextInput");
var songName = textInput.value;
if (songName == "") {
alert("Por favor, adicione um pergunta");
} else {
//alert("Adicionando a música " + SongName);
var li = document.createElement("li");
li.innerHTML = songName;
var ul = document.getElementById("playlist");
ul.appendChild(li);
}
}
HTML:
<form>
<input type="text" id="songTextInput" size="40" placeholder="Nome da música">
<input type="button" id="addButton" value="Adicionar música">
</form>
<ul id="playlist">
</ul>
The problem is: When I set my input with type=button
, this javascript works, but when I define how type=submit
, it runs legal until the first condition or with the second condition alert, but does not add the string in the list.
Does anyone know why this happens?
Give any error on the console?
– user274926
It looks like it’s reloading the page, doesn’t it? Submit submits the form (i.e., navigates to the specified URL as
action
form, or to the page itself).– bfavaretto