I have a few html image tags that fill an array. On the page – http://www.digitalbrent.com/lab – three of the elements in the array are displayed. I need to add a class to them after a button is clicked. The class is different for each if the imgs being displayed from the array. Here’s the code:
$(document).ready(function(){ //populates array with images
var shipImgs = $("#thumb_slider").children();
console.log(shipImgs);
$.each(shipImgs,function(i,elem){
var tag = "<img src='" + $(elem).attr('src') + "' alt='space'/>"; // This is how I made the image tags.
imgArr.push(tag);
});
console.log(imgArr);
});
$("#basic_ul").html(imgArr[b] + imgArr[a] + imgArr[c]); //displays the images
imgArr[b].addClass("left_slot");
imgArr[a].addClass("middle_slot");
imgArr[c].addClass("right_slot");
I’ve also tried it with the selector around the array items, $(imgArr[b]).addClass(“left_slot”); but that didn’t work either.
Any advice at all would be greatly appreciated. I’ve looked through similar questions here on stackoverflow but no luck. I’ve been researching this project for a while now and can’t find anything.
Your
imgArronly holds an array of strings of the image tags’ HTML.Instead, if you pass that string to the jQuery function, you will get an in-memory node that you can then add to the document.
Try changing your code above to: