简体   繁体   中英

javascript doesn't work to display alert

I have an html element and i want to display an alert when i click on the "i" element. When the div with the class "innerNotif" is put directly on the DOM it works but when i add the div with the socket.on it doesn't work. I think it s because the element is not in the DOM when it's load.

<span class="disNone notificationMenuCss" id="notifBar">
   <div class="innerNotif" id="idNatif">1<i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>
</span>

<script>
var socket = io.connect();
var notifId = 0;

socket.on('libCourtLotChangeServ', function(){
        var numItems = $('.innerNotif').length +1;

    $( "#notifBar" ).append( '<div class="innerNotif" id="id' + notifId +'"><i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>');
    notifId++;
    $("#notificationNumber").html(numItems);
});
</script>

<script>
    Polymer({
        alertNotif: function() {
            alert('Test');
        }
    });
</script>

The new line characters are breaking the string you want to append.

This should work:

$( "#notifBar" ).append( '<div class="innerNotif" id="id' + notifId + '"><i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>'); 

Try this code which I was edited.

<span class="disNone notificationMenuCss" id="notifBar">
   <div class="innerNotif" id="idNatif">1<i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>
</span>

<script>
var socket = io.connect();
var notifId = 0;

socket.on('libCourtLotChangeServ', function(){
        var numItems = $('.innerNotif').length +1;

    $( "#notifBar" ).append( '<div class="innerNotif" id="id' + notifId + '"><i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>');
    notifId++;
    $("#notificationNumber").html(numItems);
});
</script>

<script>
    Polymer({
        alertNotif: function() {
            alert('Test');
        }
    });
</script>

I finnaly find a solution by adding a onclick function of a parent element and add a addEventListener on it

<paper-toolbar class="header" on-click="removeNotif">
    <div class="notification" on-click="notificationMenu">
        <span class="notificationSpan" id="notificationNumber"></span>
        <span class="disNone notificationMenuCss" id="notifBar"></span>
    </div>
</paper-toolbar>

<script type="text/javascript">
var socket = io.connect();
var notifId = 0;

socket.on('libCourtLotChangeServ', function(contrat){
    var numItems = $('.innerNotif').length +1;
    $( "#notifBar" ).append( '<div class="innerNotif" id="id' + notifId +'" style="width: 96%; height: 30px; text-align: left; border: 1px solid black; background-color: white; padding: 3px;">' + 'Le contrat <br>' + contrat + ' a été modifié ' + '<i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>');
    notifId++;
    $("#notificationNumber").html(numItems);
});
</script>

<script>
 Polymer({
     removeNotif: function() {
         var idValue = Number(($(".innerNotif:last").attr("id")).replace("id", "0")); 
         console.log(idValue);
         for (var numItemsCheck = 0; numItemsCheck <= idValue; numItemsCheck++){ 
             document.querySelector("#id" + numItemsCheck).addEventListener("click", function(){
                 var numItems = $('.innerNotif').length -1;
                 $("#notificationNumber").html(numItems);
                 this.remove();
              })
          }
      }
  });
  </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM