0

My JS =>

var path = "projelerimiz/otel/balsamo/img/";
var ext = ".jpg";
var i = 1;
var img, item, figure;
for (i; i<=11;i++){     
    item = '<div class="item"></div>';
    $(item).appendTo(".swiper");
    figure = '<figure class="frame"></figure>';
    $(figure).appendTo(".item");
    img = $('<img>'); //Equivalent: $(document.createElement('img'))
    img.attr('src', path + i + ext);
    img.appendTo(".frame");
}

My HTML =>

<div class="swiper">
</div>

what I have seen when I run the JS code =>

what I saw

what I want to see when I run the JS code =>

I want to see

I need to simply loop HTML content with divs, img, etc but there are some nested loops.

Thank you.

1
  • Please state your question: what you are aiming to do, how you tried to solve it, and what is not working for you? Don't rely on just screenshots; in this case, the screenshots could be shown with code blocks. Commented Aug 4, 2017 at 13:16

6 Answers 6

2

make it simple

var path = "projelerimiz/otel/balsamo/img/";
var ext = ".jpg";
var i = 1;
var img, item, figure;
var items = [];
for(i; i <= 11; i++) {
  item = '<div class="item"><figure class="frame">' +
    '<img src="' + path + i + ext + '"></figure></div>';
  items.push(item);
}

// for best performance append element outside loop
$('.swiper').append(items.join('\n'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="swiper">
</div>

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

Comments

1

when you use .appendTo(".item"); you append to every .item elements,not the one you just created,same with .appendTo(".frame");

you can use the "inverse" function of appendTo

$(item).append(figure)//instead of $(figure).appendTo(".item");
//and
$(figure).append(img);//instead of img.appendTo(".frame");

edit:

append img to figure before appending figure to item

Comments

0

appendToadds the item to every element that matches the criteria. I suspect you only want to add the new figure and img elements to the item that you just added.

Comments

0

As the jQuery docs say,

If there is more than one target element, however, cloned copies of the inserted element will be created for each target except the last, and that new set (the original element plus clones) is returned.

So, what you're essentially doing is, foreach iteration of the for-loop, you're appending item to the .swiper div, and figure to each .item div, and img to each .frame figure. To fix this, you could change the code to:

var path = "projelerimiz/otel/balsamo/img/";
var ext = ".jpg";
var i = 1;
var img, item, figure;
for (i; i<=11;i++){     
    item = '<div class="item"></div>';
    $(item).appendTo(".swiper");
    figure = '<figure class="frame"></figure>';
    $(figure).appendTo(item);
    img = $('<img>'); //Equivalent: $(document.createElement('img'))
    img.attr('src', path + i + ext);
    img.appendTo(figure);
}

2 Comments

<div class="item swiper-slide-visible"><figure class="frame"><img src="projelerimiz/otel/balsamo/img/11.jpg"></figure></div> just need to loop this only change img source 1 2 3 .jpg
What do you mean?
0

The trick is not to use a general class selector but the variable referencing the just created element:

 var path = "projelerimiz/otel/balsamo/img/";
 var ext = ".jpg";
 var i = 1;

 var $img, $item, $figure;
 for (i; i <= 11; i++) {

   $item = $("<div />").addClass("item");
   $figure = $("<figure />").addClass("frame");
   $img = $("<img />").attr('src', path + i + ext);

   $item.appendTo(".swiper");
   $figure.appendTo($item);
   $img.appendTo($figure);
 }
.item{
  border: 1px solid black;
}

.frame{
  border: 1px solid red;
}

img{
  border: 1px solid green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="swiper">

</div>

Comments

0

To expand on the answer from @lorenzo-catalano, you could write your code something more like this:

var path = "projelerimiz/otel/balsamo/img/";
var ext = ".jpg";
var i = 1;
var swiper = $('.swiper');
var img, item, figure;

for (i; i<=11;i++){     
  item = $('<div />')
    .addClass('item');

  figure = $('<figure />')
    .addClass('frame')
    .appendTo(item);

  img = $('<img>')
    .attr('src', path + i + ext)
    .appendTo(figure);

  item.appendTo(swiper);
}

For performance, you should only update the DOM once when all items have been crea (instead of each iteration through the loop like it does now)

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.