ejemplo 2 java script

 <!DOCTYPE html>

<html lang="es">

<head>

  <meta charset="UTF-8">

  <title>Mi pequeña historia</title>

  <style>

    body {

      font-family: Arial, sans-serif;

      margin: 20px;

    }

    #cuento {

      margin-top: 20px;

      padding: 10px;

      border: 1px solid #ccc;

      background-color: #f4f4f4;

    }

  </style>

</head>

<body>

  <h2>Crea tu pequeña historia</h2>

  <form id="formulario">

    <label for="nombre">Nombre:</label>

    <input type="text" id="nombre" required><br><br>


    <label for="edad">Edad:</label>

    <input type="number" id="edad" required><br><br>


    <label for="animal">Animal favorito:</label>

    <input type="text" id="animal" required><br><br>


    <label for="deporte">Deporte favorito:</label>

    <input type="text" id="deporte" required><br><br>


    <button type="submit">Generar historia</button>

  </form>


  <div id="cuento"></div>


  <script>

    document.getElementById('formulario').addEventListener('submit', function(event) {

      event.preventDefault();


      const nombre = document.getElementById('nombre').value.trim();

      const edad = document.getElementById('edad').value.trim();

      const animal = document.getElementById('animal').value.trim().toLowerCase();

      const deporte = document.getElementById('deporte').value.trim().toLowerCase();


      const historia = `

        <strong>Hola, me llamo ${nombre}</strong>, tengo ${edad} años y esta es mi pequeña historia:<br>

        Un día, mientras caminaba por el bosque, me encontré con un <strong>${animal}</strong> muy especial. 

        ¡Era tan ágil que parecía jugar <strong>${deporte}</strong> mejor que yo!<br>

        Nos hicimos amigos y desde entonces practicamos juntos todos los días. ¡Qué aventura tan increíble!

      `;


      document.getElementById('cuento').innerHTML = historia;

    });

  </script>

</body>

</html>

Comentarios

Entradas populares de este blog

Taller hoja de cálculo

SESIÓN 1: INTRODUCCIÓN A JAVASCRIPT

Ejemplo java script