0

I have this code

 <div class="carousel-inner" role="listbox">

     <div class="item">
        <img src="http://" alt="">
        <div class="carousel-caption text-left">
          <div class="container">

          </div>
        </div>
      </div>
      <div class="item">
        <img src="http://" alt="">
        <div class="carousel-caption text-left">
          <div class="container">

          </div>
        </div>
      </div>

</div>

I want to replace only in the first div, <img src = "" alt = ""> with <video> <source src = ""> </ video>.

To clean up div I found this code. But not the first. This is the first problem.

$('.item").empty();

To write the new code to play the video, I should use this code:

$(".item").html('<video><source src=""></video>');

I can not write a good javascript code to do what I explained you. ps. I can’t change the first code because it’s a default slider of image. Thanks for the help.

1
  • To get the first div only use the :first selector. So your selector should be .item:first. Commented Oct 10, 2017 at 14:42

3 Answers 3

2

If you wish to use jquery you can utilise replaceWith() and :first pseudo selector to select and repalce only the first <img>:

$(function () {
  $('img:first').replaceWith('<video><source src=""></video>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="carousel-inner" role="listbox">

     <div class="item">
        <img src="" alt="" />
        <div class="carousel-caption text-left">
          <div class="container">

          </div>
        </div>
      </div>
      <div class="item">
        <img src="" alt="" />
        <div class="carousel-caption text-left">
          <div class="container">

          </div>
        </div>
      </div>

</div>

Bu tyou don't need Jquery to get it done. Document.querySelector() returns only the first element that matches the specified selector and the repalce it using replaceChild(https://plainjs.com/javascript/manipulation/replace-a-dom-element-36/):

var img = document.querySelector('.item img');
var video = document.createElement('video');
video.src = '';
img.parentNode.replaceChild(video, img);
<div class="carousel-inner" role="listbox">
     <div class="item">
        <img src="" alt="" />
        <div class="carousel-caption text-left">
          <div class="container">

          </div>
        </div>
      </div>
      <div class="item">
        <img src="" alt="" />
        <div class="carousel-caption text-left">
          <div class="container">

          </div>
        </div>
      </div>

</div>

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

2 Comments

Thanks a lot, I had tried with img: first but not with .replaceWith .
No problem, if you found this answer usefull please accept it
2

Try this, querySelector() selects only One selector, the first matched selector:

document.querySelector('.carousel-inner .item img').outerHTML ='<video><source src=""></video>';

1 Comment

You should explain how this selects the first element and not others.
0

You can use jQuery replaceWith() method to achieve this as below.. you can modify the selector as per your need

$('img:first').replaceWith( "<video><source src=""></video>" );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="carousel-inner" role="listbox">

     <div class="item">
        <img src="" alt="">
        <div class="carousel-caption text-left">
          <div class="container">

          </div>
        </div>
      </div>
      <div class="item">
        <img src="" alt="">
        <div class="carousel-caption text-left">
          <div class="container">

          </div>
        </div>
      </div>

</div>

3 Comments

I have said to change selector as per his requirement
Noted thanks i have updated code with correct selector now
Thanks also to you, I'm sorry, but I didn't know I could only vote once.

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.