0

I'm new in web development. I converted the list of image names to an array by following codeL

var image_array = $("#image-ul img").map(function() {return $(this).attr("src");});

Now I want to show some of these images. I show all the images by adding following codeL

$('#image-ul img').css('display','block');

But I want some of the images, like the following:

for (var i=0; i<2; i++) { $('#image-ul img:image_array[i]').css('display','block');}

How can I do this?

6
  • 1
    what means by 'some of'? Commented May 9, 2013 at 11:00
  • some from image_array Commented May 9, 2013 at 11:01
  • some of images mens which images..please specify.. it will help to give ans Commented May 9, 2013 at 11:01
  • actually i have 5 images and want to show 2 of them Commented May 9, 2013 at 11:02
  • 1
    It makes no sense to get the image source for all images in a map, and then use a for loop to select the images based on the source in a map you just got from the same elements etc? You could'nt jump thru more hoops even if you tried! Commented May 9, 2013 at 11:06

3 Answers 3

3

You don't really use a for loop to do that, you select the images and limit the selection with eq(), lt(), gt() etc. So for anyting with an index lower than 2, you do :

$('#image-ul img:lt(2)').show();
Sign up to request clarification or add additional context in comments.

Comments

1

Try

for (var i=0; i<2; i++) { $('#image-ul img:eq(' + i +')').css('display','block');}

Or

for (var i=0; i<2; i++) { $('#image-ul img').eq(i).css('display','block');}

Comments

1

Change this line

for (var i=0; i<2; i++) { $('#image-ul img:image_array[i]').css('display','block');}

to this

for (var i=0; i<2; i++) { $('#image-ul img').eq(i).show(); }

as Juhana pointed out .show() is the best way to show display an element with jQuery

2 Comments

it is my desired answer.Thanks
@andDev my example is just a way for you to get the desired effect from your code, however Adeneo has shown a much more efficient way of achieving what you're after

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.