Probleme Element = Undefined

Par MozorStudio, il y a 10 ans


Bonjour tout le monde !

Voila je debute en JS et je viens de réaliser un système de notifications en PHP. Ce que je veux c'est que ces notifications disparaissent au bout de 3 secondes.

mais le probleme c'est que mon element est: Undefined

Voila mon code:

JS

var notification = document.getElementsByClassName('notification-container'); setTimeout(function(){ notification.style.right = "-60px"; }, 2900); setTimeout(function(){ notification.style.opacity = 0; }, 3000);around

HTML

<div class="notification-container"> <div class="notification-block notification-success" id="success"> <li class="notification">Ma notif</li> </div> </div>

Voici mes erreurs:
Uncaught TypeError: Cannot set property 'right' of undefined(anonymous function) | ET | Uncaught TypeError: Cannot set property 'opacity' of undefined

2 réponses

alexo4, il y a 10 ans

Salut,

Ton script se charge surement avant l'affichage de ta div "notification-container", d'où le undefined.

betaWeb, il y a 10 ans

Salut,

L'erreur parle d'elle-même: ton élément est undefined.
Pourquoi n'utilises-tu pas jQuery plutôt ? C'est bien plus simple pour faire ce genre d'opérations.

Sinon ce que tu peux essayer de faire, c'est ceci:
HTML:

<div id="notification-container"> <ul class="notification-block notification-success" id="success"> <li class="notification">Ma notif</li> </ul> </div>

JS:

var notif = document.getElementsById('notification-container'); setTimeout(function(){ notif.style.right = "-60px"; }, 2900); setTimeout(function(){ notif.style.opacity = 0; }, 3000);