0

How to grab the image of the first swiper-slide to insert into the div with a class of tt1?

Only image of first .swiper-slide!

Ex:

<div class="tt1"></div>    
<div class="ss1">...</div>
<div class="ee1">
    <div class="ee2">
        <div class="swiper-slide">
            <img src="/image1.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image2.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image3.jpg" style="width: 225px; height: 400px;">
        </div>
    </div>
</div>

Result:

<div class="tt1"><img src="/image1.jpg"></div>    
<div class="ss1">...</div>
<div class="ee1">
    <div class="ee2">
        <div class="swiper-slide">
            <img src="/image1.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image2.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image3.jpg" style="width: 225px; height: 400px;">
        </div>
    </div>
</div>

Or I could take the first image to form the background?

<div class="tt1" style="background: url(/image1.jpg);"></div>    
<div class="ss1">...</div>
<div class="ee1">
    <div class="ee2">
        <div class="swiper-slide">
            <img src="/image1.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image2.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image3.jpg" style="width: 225px; height: 400px;">
        </div>
    </div>
</div>
1

4 Answers 4

3

You could use:

$('.swiper-slide:first img').clone().appendTo('div.tt1')

jsFiddle example

This selects the first element of the class swiper-slide, then the image, copies it and appends it to the div with class tt1. The resulting HTML is:

<div class="tt1"><img src="/image1.jpg" style="width: 225px; height: 400px;"></div>    
<div class="ss1">...</div>
<div class="ee1">
    <div class="ee2">
        <div class="swiper-slide">
            <img src="/image1.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image2.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image3.jpg" style="width: 225px; height: 400px;">
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

2
$('.tt1').append($('.swiper-slide:first-child img').clone())

Comments

1

SOLUTION:

To achieve this you will use a CSS selector to target your first image, which will later be used as your jQuery selector.

You'll need to use the pseudo-class :first-child, later on create a clone using clone() jQuery method and finally appending that clone to the desired div element using the appendTo() method.


JSFiddle


Image being cloned surrounded in red. Image cloned and appended surrounded in blue.


CODE SNIPPET:

$(".swiper-slide:first-child img").clone().appendTo($(".tt1"));
.tt1 img {
  border: 3px solid blue;
  display: inline-block;
}
.swiper-slide img {
  width: 225px;
  height: 400px;
}
.ee2 .swiper-slide:first-child img {
  border: 3px solid tomato;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tt1"></div>
<div class="ss1">...</div>
<div class="ee1">
  <div class="ee2">
    <div class="swiper-slide">
      <img src="http://placehold.it/225x400">
    </div>
    <div class="swiper-slide">
      <img src="http://placehold.it/225x400">
    </div>
    <div class="swiper-slide">
      <img src="http://placehold.it/225x400">
    </div>
  </div>
</div>


As a side note: I recommend removing these inline-style css properties and using them in an external css file.

Replace style attribute:

style="width: 225px; height: 400px;"

With:

.ee2 .swiper-slide img {
  width: 225px;
  height: 400px;
}

(In an External CSS file)

2 Comments

You can remove style of image ?
Yes, I will suggest setting width and height in an external css file rather than using inline-style. Will edit.
0

You can use $('.swiper-slide:eq(1) img').clone().appendTo('div.tt1')

<div class="tt1"></div>    
<div class="ss1">...</div>
<div class="ee1">
    <div class="ee2">
        <div class="swiper-slide">
            <img src="/image1.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image2.jpg" style="width: 225px; height: 400px;">
        </div>
        <div class="swiper-slide">
            <img src="/image3.jpg" style="width: 225px; height: 400px;">
        </div>
    </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
   $('.swiper-slide:eq(1) img').clone().appendTo('div.tt1')
   $('.tt1 img').removeAttr('style');
</script>

6 Comments

How's this any different from the answer I posted?
You are using .swiper-slide:first and i am using .swiper-slide:eq(1). Different ways of selectors is there may be these simple differece will help you to some one. eq(2),eq(3) also we can use for next elements.
You can remove style of image ?
:eq(1) is the same as :first so your answer is the same
|

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.