Advertisement
fcamuso

Corso recupero javascript lezione 19

Jan 14th, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="it">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>Document</title>
  7.  
  8.   <link rel="stylesheet"
  9.         href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  10. </head>
  11. <body>
  12.  
  13.   <div class="container mt-5">
  14.     <h1 class="mb-4">Gestione Paragrafi</h1>
  15.  
  16.     <button id="inserisci"   class="btn btn-primary mb-3">Inserisci Paragrafo</button>
  17.     <button id="elimina" class="btn btn-danger mb-3">Elimina Paragrafo</button>
  18.    
  19.     <div id="container_paragrafi">
  20.       <!-- qui saranno inseriti i paragrafi -->
  21.  
  22.     </div>
  23.  
  24.   </div>
  25.  
  26.  
  27.   <script
  28.     src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
  29.   </script>
  30.      
  31.   <script>
  32.     let conta_paragrafi = 0;
  33.  
  34.     document.getElementById('inserisci').addEventListener('click', function() {
  35.       const container_paragrafi = document.getElementById('container_paragrafi');
  36.      
  37.       const nuovo_paragrafo = document.createElement('p');
  38.       conta_paragrafi++;
  39.  
  40.       nuovo_paragrafo.textContent = conta_paragrafi + ' creat' +
  41.                                     (conta_paragrafi > 1 ? 'i' : 'o');
  42.      
  43.       // nuovo_paragrafo.className =  
  44.       //   conta_paragrafi%2===0 ? 'alert alert-info ' : 'alert alert-warning';
  45.                                    
  46.       nuovo_paragrafo.className =  
  47.           conta_paragrafi%2===0 ? 'text-primary' : 'text-danger';
  48.  
  49.      
  50.      
  51.       container_paragrafi.appendChild(nuovo_paragrafo);
  52.  
  53.     });
  54.  
  55.  
  56.     document.getElementById('elimina').addEventListener('click', function() {
  57.       let paragrafi = document.getElementById('container_paragrafi').children;
  58.  
  59.       if (paragrafi.length>0) {
  60.         paragrafi[0].remove();
  61.         conta_paragrafi--;
  62.  
  63.         paragrafi = document.getElementById('container_paragrafi').children;
  64.        
  65.         for (let i=0; i<paragrafi.length; i++) {  
  66.           paragrafi[i].textContent = (i+1) + ' creat' + (i>0 ? 'i' : 'o');
  67.           //paragrafi[i].className = (i+1)%2===0 ? 'alert alert-info' : 'alert alert-warning';
  68.  
  69.           paragrafi[i].className = (i+1)%2===0 ? 'text-primary' : 'text-danger';
  70.         }
  71.       }
  72.     });
  73.   </script>
  74. </body>
  75. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement