0

I'm trying to transform div content from img to div strong

for example

<div class="multi-gallery-image show" id="service_preview">
    <img alt="image" src="チャイルドカット¥3000">
    <img alt="image" src="ジュニアカット">
    <img alt="image" src="ハナコカット">
    <img alt="image" src="Hair Styling">
    <img alt="image" src="Manicures">
    <img alt="image" src="Hair Coloring">
</div>

I want to transform to div strong

<div class="multi-gallery-image show" id="service_preview">
     <div><strong>チャイルドカット¥3000</strong></div>
     <div><strong>ジュニアカット</strong></div>
     <div><strong>トップスタイリストカット</strong></div>
     <div><strong>Hair Styling</strong></div>
     <div><strong>Manicures</strong></div>
     <div><strong>Hair Coloring</strong></div>
</div>

I have this but the result is different as expected:

let servicePreview = document.getElementById('service_preview');     //parent where I am trying to introduce the src values
let myImg;
let mySrc;
let toPush;

if (servicePreview.getElementsByTagName('img').length > 0){
    let servicesNumber = servicePreview.getElementsByTagName('img').length;     //number of img tags inside the service_preview
    for (let i = 0; i < servicesNumber; i++){
        myImg = servicePreview.getElementsByTagName('img')[i];      //capturing the img tag
        mySrc = myImg.getAttribute('src');              //capturing the src value from img tag
        toPush = '<div><strong>' + mySrc + '</strong></div>';      //creating html tag for push to the parent service_preview
        servicePreview.append(toPush);                            //appending the html tag
    }
}

but the result of this is

<div class="multi-gallery-image show" id="service_preview">
    <img alt="image" src="チャイルドカット¥3000">
    <img alt="image" src="ジュニアカット">
    <img alt="image" src="ハナコカット">
    <img alt="image" src="トップスタイリストカット">
    <img alt="image" src="Hair Styling">
    <img alt="image" src="Manicures">
    
    "<div><strong>チャイルドカット¥3000</strong></div>"
    "<div><strong>ジュニアカット</strong></div>"
    "<div><strong>ハナコカット</strong></div>"
    "<div><strong>トップスタイリストカット</strong></div>"
    "<div><strong>Hair Styling</strong></div>"
    "<div><strong>Manicures</strong></div>"
</div>
  • I want to delete that "quotes" on every div strong, that is a string.
  • I have to delete the complete img tag after solve the "quotes" problem

enter image description here

5 Answers 5

2

use createElement to create dom element to appendChild and removeChild to remove elements.

let servicePreview = document.getElementById('service_preview');
var myImg;
var mySrc;
let toPush;

var elements = servicePreview.getElementsByTagName('img');
while (elements[0]) {
  newDiv = document.createElement('div');
  newStrong = document.createElement('strong');
  newStrong.innerHTML = elements[0].getAttribute('src');
  newDiv.appendChild(newStrong);
  servicePreview.appendChild(newDiv);
  elements[0].parentNode.removeChild(elements[0]);
}
<div class="multi-gallery-image show" id="service_preview">
    <img alt="image" src="チャイルドカット¥3000">
    <img alt="image" src="ジュニアカット">
    <img alt="image" src="ハナコカット">
    <img alt="image" src="Hair Styling">
    <img alt="image" src="Manicures">
    <img alt="image" src="Hair Coloring">
</div>

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

1 Comment

thanks! I changed document.getElementsByTagName('img') to servicePreview.getElementsByTagName('img') to avoid to return all url images
1

Unlike jQuery append() the native method treats html strings as text

Use insertAdjacentHTML(position, html) instead

const str = '<div class="inserted">Test</div>'

document.getElementById('one').append(str);// shows as text
document.getElementById('two').insertAdjacentHTML('beforeend', str)
.inserted{color:red}
<div id="one"></div>
<div id="two"></div>

Comments

1

In raw JavaScript I believe its something like this.

const services = ['チャイルドカット¥3000', 'ジュニアカット', 'ハナコカット',
  'Hair Styling', 'Manicures', 'Hair Coloring']

const preview = document.getElementById("service_preview")

services.forEach(service =>{
  const div = document.createElement("div")
  const strong = document.createElement("strong")
  strong.innerText = service
  div.appendChild(strong)
  preview.appendChild(div)
})

Comments

1
const servicePreview = document.getElementById("service_preview");
const imageSources = [...servicePreview.children].map((img) => img.src);
console.log(imageSources);

// Remove all images
while (servicePreview.firstChild) {
  servicePreview.removeChild(servicePreview.firstChild);
}

// Add new div/strong tags
for (const imageSource of imageSources) {
  const div = document.createElement("div");
  const strong = document.createElement("strong");

  strong.textContent = imageSource;

  div.appendChild(strong);
  servicePreview.appendChild(div);
}

Comments

1

simply use replaceChild() method

there is a trap with img.src because you get an URI

const servicePreview = document.getElementById('service_preview')

servicePreview.querySelectorAll('img').forEach(imgElm=>
  {
  let newDiv  = document.createElement('div')
    , newStrg = document.createElement('strong')
    ;
  newDiv.appendChild( newStrg )
  newStrg.textContent = imgElm.getAttribute('src')  // or decodeURI( imgElm.src.split('/').pop() ) 

  servicePreview.replaceChild( newDiv, imgElm )
  })
<div class="multi-gallery-image show" id="service_preview">
  <img alt="image" src="チャイルドカット¥3000">
  <img alt="image" src="ジュニアカット">
  <img alt="image" src="ハナコカット">
  <img alt="image" src="Hair Styling">
  <img alt="image" src="Manicures">
  <img alt="image" src="Hair Coloring">
</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.