1

I am trying to map two data "points" together, which do not really have anything in common. I have an array which contains images at certain positions, within the array. I also have a variable that can contain various values (1, 2 or 3), and I want to map the array position (1, 2 or 3) and the variable value, to show the image that is in the specific array position.

<script>

var images = new Array()
images[1] = 'image/position/in/folder/image.jpg';
images[2] = 'image/position/in/folder/image.jpg';
images[3] = 'image/position/in/folder/image.jpg';

var var_name = somedata


/*----here I need help or guidance----

jQuery(document).ready(function() {
   var jQuerydiv = jQuery(".image_div");

   jQuery.each(images, function(i, val) {

      jQuery("<img />").attr("src", val).appendto(jQuerydiv);

   });
});


/*------guidance/help ENDS------------

<script>

<div class="image_div"></div>

This script throws all images in the array "images" to div "image_div". I want to only show the image in the array, which position (1, 2 or 3), matches the value of variable var_name, which can contain 1, 2 or 3.

Anything obliged.

2 Answers 2

1
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 <script> 
    var images = new Array() 
    images[1] = 'image1.png';
    images[2] = 'image2.png';
    images[3] = 'image3.png';

    // console.log(images);
    // [empty × 1, "image1.png", "image2.png", "image3.png"]
    // here images are put at index 1,2,3
    var var_name = 1;

    $(document).ready(function() {
      var jQuerydiv = $(".image_div");

    $.each(images, function(i, val) {
    // now append image at index 1 to div
    if(i == var_name){
        $("<img />").attr("src", val).appendTo(jQuerydiv);
    }
   });
});
</script>

<div class="image_div"></div>
Sign up to request clarification or add additional context in comments.

4 Comments

I tried your way and I get the same kinda....if implement your if statement, nothing happens...and if I comment out just the if statement, then all three images are appended to div...
data contains 1,2,3 index so in if statement use i instead of val.
Please don't just dump code. Add an explanation how this code works and why it might be a solution to OP's problem.
now code is self explanatory. Use this and let us know result i tested and it is working.
0

Use appendTo and access the images array from 0. Also close script tag

img
{
border: 1px solid #000;
  width: 50px;
  height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>

var images = new Array()
images[0] = 'image/position/in/folder/image.jpg';
images[1] = 'image/position/in/folder/image.jpg';
images[2] = 'image/position/in/folder/image.jpg';


jQuery(document).ready(function() {
   var jQuerydiv = jQuery(".image_div");

   jQuery.each(images, function(i, val) {

      jQuery("<img />").attr("src", val).appendTo(jQuerydiv);

   });
});

</script>

<div class="image_div"></div>

Comments

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.