1

i am new to js. i am trying to grab a image from an array with similar class names which has a specific src saved in a variable bigImgPath

here is my Updated Code..

  var moniqueThumbs=document.getElementsByClassName('moniqueThumbs'); // Grab the ThumbNails
var bigImagesList=document.getElementsByClassName('monique-image');  // Grabs All Big Images

var currentBigImg='';

var currentBigImageFilePath="";  // Current Big Image path Captured by current Thumb Click //s
for(var i = 0; i <  moniqueThumbs.length; i++){
     moniqueThumbs[i].addEventListener("click", grabBigImgPath); // Added a myFunction Click event to Thumbs
    }




        // Grab Big Image Path from Clicked Thumb
        function grabBigImgPath() 
        {
    currentBigImageFilePath=this.getAttribute('data-bigImgPath'); // grabs the current bigPath from the thumb//

    var currentBigImageToDisplay;
    for (var i = 0; i < bigImagesList.length; i++)
    {
        if (bigImagesList[i].getAttribute('src') == currentBigImageFilePath)
        {
            currentBigImageToDisplay = bigImagesList[i];
            console.log(currentBigImageToDisplay);

            break;
        }
    }


}

Still not displaying the current image in console.log

2
  • could you please show your array Commented Aug 21, 2015 at 13:03
  • var bigImagesList=document.getElementsByClassName('monique-image'); // Grabs All Big Images Commented Aug 21, 2015 at 13:06

1 Answer 1

1

You need to use the getAttribute method to get the src:

currentBigImageToDisplay = bigImagesList[i].getAttribute('src') == bigImgPath;

That's still not right however, as it will just return true or false, so you want something like:

var currentBigImageToDisplay;
for (var i = 0; i < bigImagesList.length; i++) {
    if (bigImagesList[i].getAttribute('src') == bigImgPath) {
        currentBigImageToDisplay = bigImagesList[i];
        break;
    }
}

Alternatively, you could use the filter syntax:

bigImagesList = Array.prototype.slice.call(bigImagesList);
var currentBigImageToDisplay = bigImagesList.filter(function(item) {
    return item.getAttribute('src') == bigImgPath;
})[0]

Working JSFIddle

Sign up to request clarification or add additional context in comments.

9 Comments

it says Uncaught ReferenceError: bigImageList is not defined
@MarcAndreJiacarrini Typo, should be fixed.
ok now i added console.log(currentBigImageToDisplay); but nothing is shown in console... :(
@JamesBrierley iterating over a NodeList with filter is not possible. You have to convert the object to an Array first.
@MarcAndreJiacarrini I've added a working JSFiddle example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.