1
I’m adding a reason, until then ok, delete the first reason, but the added ones I can’t delete!
var antes = 'Bem estar psicológico <img src="delete.png" style="width: 1cm" onclick="deleteMot(' + '"Bem estar psicológico"' + ');"> <br>';
function addMotivo() {
var mot = document.getElementById("newMotivo").value;
if (mot != "") {
antes = antes + mot + ' <img src="delete.png" style="width: 1cm" onclick="deleteMot("' + mot + '");"> <br>';
document.getElementById("motivos").innerHTML = antes;
document.getElementById("newMotivo").value = "";
} else {
window.alert("Motivo inválido");
}
}
function deleteMot(i) {
if (confirm("Tem a certeza que quer apagar?")) {
var r = i + ' <img src="delete.png" style="width: 1cm" onclick="deleteMot("' + i + '");"> <br>';
antes = antes.replace(r, '');
document.getElementById("motivos").innerHTML = antes;
}
}
fieldset {
border: none;
}
label {
display: block;
cursor: pointer;
}
.remove {
color: black;
font-weight: bold;
text-decoration: none;
}
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
</head>
<body>
<div class="infor" style="clear: both;"><b>Motivos</b></div>
<div class="infor" style="clear: both; margin-top: 7px;">
<input type="text" id="newMotivo" placeholder="Ficar atraente">
<button onclick="addMotivo();" style="width: auto">Adicionar Motivo: </button>
</div>
<div class="infor" id="motivos" style="clear: both; margin-top: 7px; color: #CC720E;"><b>Bem estar psicologico</b>
<img src="delete.png" style="width: 18px" onclick="deleteMot('Bem estar psicológico');" alt="" />
</div>
</body>
</html>
obd david by editing
– Evandro Aguiar
never use
getElementById
usegetElementsByClassName
that would be:document.getElementsByClassName("motivo" + i)
– Lollipop
The problem is because you are trying to access an identifier that is duplicated, or tripled... It needs to be a class in order to be multiplied.
– Lollipop
how do I do it Lollipop
– Evandro Aguiar