<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <body> <div class="container "> <div class="form-group"> <label for="cadena">Cadena original:</label> <input type="text" class="form-control" id="cadena"> </div> <div class="form-group"> <label for="cadena">Cadena a eliminar:</label> <input type="text" class="form-control" id="quitar"> </div> <h1 id="posicion"></h1> <button type="button" class="btn btn-success" onclick="quitar()">Buscar</button> <script src="js/test.js"></script> </form> </div> </body> </html>
function quitar () { let cadena = document.getElementById('cadena').value const quitar = document.getElementById('quitar').value const pos = cadena.indexOf(quitar) if (pos > -1) { cadena = cadena.slice(0, pos) + cadena.slice(pos + quitar.length) } document.getElementById('posicion').innerHTML = cadena }