0

I need to send an alert to the user every time the z-index equals 2. Unfortunately it only occurs onload, or ready...whatever...

heres the html

<div id='slides'>
   <img class='sliderImg' src='img.jpg'>
   <img class='sliderImg' src='img.jpg'>
   <img class='sliderImg' src='img.jpg'>
</div>

and the Javascript

document.ready=function(){
   var theImage=$('.sliderImg')[0];
   if(theImage.style.zIndex==2){
      alert(theImage.style.zIndex);
   }
}
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101

3 Answers3

0

You have two choices:

1) Run a timer and call your function periodically, using setInterval or setTimeout

2) Listen for DOM changes, then run your function.

Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

You can use setInterval function after t time to check the z-index like this :

window.setInterval(function(){
   var theImage=$('.sliderImg')[0];
   if(theImage.style.zIndex==2){
      alert(theImage.style.zIndex);
   }
},10000);

It will be called on every 10 seconds.

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
0

jQuery style :

document.ready = function(){
   checkZIndexOfImage(zIndex);
   window.setInterval(checkZIndexOfImage, 10000);
}

function checkZIndexOfImage(zIndex) {
   var theImage=$('.sliderImg').first(); //or $('.sliderImg').eq(0)
   var tempzIndex = theImage.style('z-index');
   tempzIndex = parseIng(zIndex);
   if (isNaN(tempzIndex)) { //Let's check z-index is number
     tempzIndex = 0;
   }
   if(tempIndex === zIndex){
      alert(tempzIndex );
   }
}
d.danailov
  • 9,594
  • 4
  • 51
  • 36