IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Création de bouton en javascript


Sujet :

JavaScript

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2019
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2019
    Messages : 1
    Points : 0
    Points
    0
    Par défaut Création de bouton en javascript
    Je n'arrive pas à faire fonctionner mes boutons(ajouter, modifier, supprimer, annuler) en Javascript.
    Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    <!DOCTYPE html>
    <html>
     
    <head>
        <title>Tableau interactif</title>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="./css/style.css" />
        <script src="js/script.js"></script>
        <script src="https://kit.fontawesome.com/a076d05399.js"></script>
     
    </head>
     
    <body>
        <div>
            <table>
                <thead>
                    <tr>
                        <th>Numéro</th>
                        <th>Nom <button onclick="TrierNon()" class="triernom"><i class="fas fa-sort"></i></button></th>
                        <th>Prénom <button onclick="TrierPrenom()" class="trierprenom"><i class="fas fa-sort"></i></button>
                        </th>
                        <th>Moyenne</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <td>1</td>
                    <td>Camara</td>
                    <td>Salian</td>
                    <td>74</td>
                    <td><button onclick="modifierLine()" class="BouttonModi"><i class="fa fa-edit"
                                style="font-size:14px"></i></button> <button onclick="supprimerLine()" class="BouttonSup"><i
                                class="fa fa-trash" style="font-size:14px"></i></button><button onclick="annulerAction()"
                            class="BouttonAnnul"><i class="fa fa-ban" style="font-size:14px"></i></button></td>
     
                    </tr>
                    <tr>
                        <td>2</td>
                        <td>Barry</td>
                        <td>Mamadou</td>
                        <td>54</td>
                        <td><button onclick="modifierLine()" class="BouttonModi"><i class="fa fa-edit"
                                    style="font-size:14px"></i></button> <button onclick="supprimerLine()"
                                    class="BouttonSup"><i class="fa fa-trash" style="font-size:14px"></i></button><button
                                    onclick="annulerAction()" class="BouttonAnnul"><i class="fa fa-ban"
                                style="font-size:14px"></i></button></td>
                    </tr>
                    <tr>
                        <td>3</td>
                        <td>Kaba</td>
                        <td>Fatoumata</td>
                        <td>66</td>
                    </tr>
                    <tr>
                        <td>4</td>
                        <td>Bangoura</td>
                        <td>Saran</td>
                        <td>99</td>
                    </tr>
                    <tr>
                        <td>5</td>
                        <td>Haba</td>
                        <td>Félix</td>
                        <td>45</td>
                    </tr>
                    <tr>
                        <td>6</td>
                        <td>Sylla</td>
                        <td>Fanta</td>
                        <td>35</td>
                    </tr>
                    <tr>
                        <td>7</td>
                        <td>Conde</td>
                        <td>Ousmane</td>
                        <td>90</td>
                    </tr>
                    <tr>
                        <td>8</td>
                        <td>Sow</td>
                        <td>Abdourahmane</td>
                        <td>75</td>
     
                    </tr>
                    <tr>
                        <td>9</td>
                        <td>Camara</td>
                        <td>Kabinet</td>
                        <td>83</td>
                    </tr>
                    <tr>
                        <td>10</td>
                        <td>Barry</td>
                        <td>Abdoulaye</td>
                        <td>91</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
     
                        <th><input type="number" name="numero" id="numero" /> </th>
                        <th><input type="text" name="nom" id="nom" /></th>
                        <th><input type="text" name="prenom" id="prenom" /></th>
                        <th><input type="number" name="note" id="note" /></th>
                        <th><button onclick="AjouterLigne();">Ajouter</button></th>
                    </tr>
                    <!--<td><button onclick="AjouterLigne()" class="BouttonAjout">Ajouter</button></td>-->
                </tfoot>
            </table>
            <div class="dropdown">
                <button onclick="myFunction()" class="dropbtn"><i class="fa fa-edit" style="font-size:14px"></button>
                <div id="myDropdown" class="dropdown-content">
                    <a href="#ajouter"><i class="fa fa-edit" style="font-size:14px"></a>
                    <a href="#supprimer"><i class="fa fa-trash" style="font-size:14px"></i></a>
                    <a href="#annuler"><i class="fa fa-ban" style="font-size:14px"></i></a>
                </div>
            </div>
     
     
        </div>
    </body>
    <style>
    table{
        border-collapse: collapse;
        width: 100px;
        background: rgb(197, 194, 194);
        max-width: 900px;
        margin: auto;
        padding: 10px;
        align-self: center;
    }
    th, td{
        text-align: center;
        padding: 8px;
    }
    tr:nth-child(even){
        background-color: #f2f2f2
    }
    th{
        background-color: #4CAF50;
        color: white;
    }
    </style>
    <script>
    function myFunction(){
    document.getElementById("myDropdown").classList.toggle("show");
    }
     
     
    /*function init()
    {
        let count = document.querySelectorAll("tbody tr").length;
        document.getElementById("identifiant").value = count+1;
       
        let btn = document.getElementById("sendButton");
        btn.onclick=function(evt)
        {
           let element = document.getElementById("titre");
           let value = element.value;
           console.log(value);
        }
    }
    window.onload = init;*/
        var rIndex,
        table = document.getElementById("table");
     
        function verifiequelavaleurvide()
        {
            var isEmpty=false,
            fnumber = document.getElementById("fnumber").value,
            fnom = document.getElementById("fnom").value,
            fprenom = document.getElementById("fprenom").value,
            fnote = document.getElementById("fnote").value;
            if(numbero==="")
            {
                alert("Le numéro ne peut être vide");
                isEmpty=tue;
            }
            else if(nom==="")
            {
                alert("Le nom ne peut être vide");
                isEmpty=true;
            }
            else if(prenom==="")
            {
                alert("Le prénom ne peur être vide");
                isEmpty=true;
            }
            else if(note==="")
            {
                alert("Veuillez entrer une note.");
                isEmpty=true;
            }
            return isEmpty;
        }
        function AjouterLigne()
        {
            if(!checkEmptyInput()){
                var newRow = table.insertRow(table.length),
                var table = document.getElementById("table"),
                    newRow = table.insertRow(table.length),
                    cell1 = newRow.insertCell(0)
                    cell2 = newRow.insertCell(1)
                    cell3 = newRow.insertCell(2)
                    cell4 = newRow.insertCell(3)
                    numebro = document.getElementById("numbero").value,
                    nom = document.getElementById("nom").value,
                    prenom = document.getElementById("prenom").value,
                    note = document.getElementById("note").value;
        
                cell1.innerHTML = numero;
                cell2.innerHTML = nom;
                cell3.innerHTML = prenom;
                cell4.innerHTML = note;
                SelectedRowToInput();
            }
        }
        function SelectedRowToInput()
        {
            for(var i=1; i<table.rows.length; i++)
            {
                table.rows[i].onclick=function()
            }
        }
        function TrierNon() {
            // Initialise chaque tableau de classe « avectri »
               [].forEach.call( document.getElementsByClassName("avectri"), function(triernom) {
               var oEntete = oTableau.getElementsByTagName("tr")[0];
               var nI = 1;
                // Ajoute à chaque entête (th) la capture du clic
                // Un picto flèche, et deux variable data-*
                // - Le sens du tri (0 ou 1)
                // - Le numéro de la colonne
              [].forEach.call( oEntete.querySelectorAll("th"), function(oTh) {
                oTh.addEventListener("click", twTriTableau, false);
                oTh.setAttribute("data-pos", nI);
                if(oTh.getAttribute("data-tri")=="1") {
                 oTh.innerHTML += "<span class=\"flecheDesc\"></span>";
                } else {
                  oTh.setAttribute("data-tri", "0");
                  oTh.innerHTML += "<span class=\"flecheAsc\"></span>";
                }
                if (oTh.className=="selection") {
                  oTh.click();
                }
                nI++;
              });
            });
          }
          
          function TrierNon() {
            TrierNon();
          }
          function twPret(maFonction) {
            if (document.readyState != "loading"){
              maFonction();
            } else {
              document.addEventListener("DOMContentLoaded", maFonction);
            }
          }
          twPret(triernom);
        
          function TrierNon() {
            var nBoolDir = this.getAttribute("data-tri");
            this.setAttribute("data-tri", (nBoolDir=="0") ? "1" : "0");
            [].forEach.call( this.parentNode.querySelectorAll("th"), function(oTh) {oTh.classList.remove("selection");});
            this.className = "selection";
            this.querySelector("span").className = (nBoolDir=="0") ? "flecheAsc" : "flecheDesc";
        
            var oTbody = this.parentNode.parentNode.parentNode.getElementsByTagName("tbody")[0]; 
            var oLigne = oTbody.rows;
            var nNbrLigne = oLigne.length;
            var aColonne = new Array(), i, j, oCel;
            for(i = 0; i < nNbrLigne; i++) {
              oCel = oLigne[i].cells;
              aColonne[i] = new Array();
              for(j = 0; j < oCel.length; j++){
                aColonne[i][j] = oCel[j].innerHTML;
              }
            }
        
            var nIndex = this.getAttribute("data-pos");
            var sFonctionTri = (this.getAttribute("data-type")=="num") ? "compareNombres" : "compareLocale";
            aColonne.sort(eval(sFonctionTri));
            function compareNombres(a, b) {return a[nIndex-1] - b[nIndex-1];}
            function compareLocale(a, b) {return a[nIndex-1].localeCompare(b[nIndex-1]);}
            if (nBoolDir==0) aColonne.reverse();
            
            for(i = 0; i < nNbrLigne; i++){
              aColonne[i] = "<td>"+aColonne[i].join("</td><td>")+"</td>";
            }
        
            oTbody.innerHTML = "<tr>"+aColonne.join("</tr><tr>")+"</tr>";
          }
    </script>
    </html>

  2. #2
    Inactif  
    Femme Profil pro
    Analyste d'exploitation
    Inscrit en
    Septembre 2019
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Turquie

    Informations professionnelles :
    Activité : Analyste d'exploitation
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2019
    Messages : 1
    Points : 6
    Points
    6
    Par défaut création de bouton en javascript
    Il est tout à fait possible de créer des boutons en javascript, et d'interagir avec le DOM de ta page en générale.

    La méthode dont tu as besoin est createElement. :magicien:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    var btn = document.createElement("BUTTON");        // Créer un élément <button>
    var t = document.createTextNode("CLICK ME");       // Créer un noeud textuel
    btn.appendChild(t);                                // Ajouter le texte au bouton
    document.body.appendChild(btn);                    // Ajoute la balise <button> à la balise <body>
    PS : Si tu est bloqué, n'hésite pas à demander de l'aide.

Discussions similaires

  1. création de boutons avec javascript
    Par cosmos33 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 15/01/2007, 10h48
  2. [VBA-E]Création de boutons dans VB
    Par BBe8127 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 04/05/2006, 16h33
  3. [VBA-E]Création de boutons selon une liste
    Par frevale dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 25/03/2006, 14h31
  4. [FLASH MX2004] Création de boutons avec données php
    Par joesature dans le forum Flash
    Réponses: 1
    Dernier message: 06/10/2005, 13h06
  5. désactiver un bouton en Javascript
    Par michaelbob dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 30/05/2005, 15h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo