tags:

views:

39

answers:

2

I'm working on a simple if/else statement to show a next or previous link, but somehow my checkup don't work. Here's the code


var newPosition = currPosition;
$(".imageNavLink").click(function(event){
 event.preventDefault();
 newPosition = (this.rel == "next") ? newPosition + 1 : newPosition - 1;
 var newImageLink = this.title;
 var clickAction = this.rel;
 var newImage = new Image();
 $(newImage).load(function(){
  newWidth = this.width, newHeight = this.height+35;
  if(newPosition == totalItems){
   [.. Show 'previous' link ..]
  }else if(newPosition == 1){
   [.. Show 'next' link ..]
  }else{
   [.. Show 'previous' and 'next' link ..]
  }
 });
 newImage.src = this.title;
});
  • currPosition holds the current position (numeric) of the element to keep on track with the count
  • totalItems is the numeric value of the total associated images

If i click on the first image i constantly see the previous link, when i start with the second image i constantly see both links and when i start with the last image i constantly see the previous link..

A: 

I dont know if this is it but,

newWidth = this.width, newHeight = this.height+35;

Shouldn't the , after this.width be a ; ?

Tom
More likely he wanted a `var` in front of it. But that's not the problem...
T.J. Crowder
Not the problem indeed. Thanks anyway! :)
Maurice
A: 
 if(newPosition == totalItems){
   [.. Show 'previous' link ..]
  }else if(newPosition == 1){
   [.. Show 'next' link ..]
  }else{
   [.. Show 'previous' and 'next' link ..]
  }

rewrite to this:

 if(newPosition>1 && newPosition<totalItems){
   [.. Show 'previous' and 'next' link ..]
  }else if(newPosition == 1){
   [.. Show 'next' link ..]
  }else if(newPosition == totalItems){
   [.. Show 'previous' link..]
  }

Only if newPosition is between 1 and totalItems will both prev and next be shown

dany
I'm starting to think that the problem is somewhere else, because if i now click the first image i don't see anything. When i click the second i only get the previous link and also if i click the last image i see the previous button... The problem is that there is no relative (at least i think) around it..
Maurice
is the value `totalImages` changing everytime you click an item?
dany
Nope. It is defined above and stays the same everytime.. I check everything with an <code>alert(currPosition+" -> "+newPosition+" -> "+totalItems);</code> and everything seems to change well there... newPosition is the only one that changes..
Maurice