D
D
DivineDraft2018-02-23 10:20:44
JavaScript
DivineDraft, 2018-02-23 10:20:44

How to wrap an image with the desired code?

Construction in progress:

<div class="news-full-text">
текст
<img src="#" свойств и тд>
текст
<img src="#" свойств и тд>
текст
</div>


How can I access the .news-full-text img and wrap each of the images with example code to wrap around the img:
<div class="image">
    <img src="картинка">
    <div class="image-social">социальные кнопки</div>
</div>


Thank you for any examples or thoughts.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-02-23
@DivineDraft

$('.news-full-text img')
  .wrap('<div class="image"></div>')
  .after('<div class="image-social">социальные кнопки</div>');

or
$('.news-full-text img').replaceWith(function() {
  return `
    <div class="image">
      ${this.outerHTML}
      <div class="image-social">социальные кнопки</div>
    </div>`;
});

or
$('.news-full-text').html((i, html) => html.replace(/<img .*?>/g, m => `
  <div class="image">
    ${m}
    <div class="image-social">социальные кнопки</div>
  </div>
`));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question