Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
why javascript removes forward slash from image url, how I can fix it?

I am writing some HTML code inside JavaScript where I also used inline css to add background-image using background-img property. But on browser image is not showing and when I checked in console image path is doesn't have forward slash.

let cardsObject = 
[
    {
        title:"FMCG-F&B", discription:"marketing and business 
        planning",imgPath:"./images/firm3.jpg"
    } 

]

function cardShow() {
let featCards =""; 
cardsObject.forEach(cObj=>{
    
    featCards+=
    `
    <div class="card mr-2 mt-2"
    style=
    "
    background-image: url("${cObj.imgPath}");
    background-size: cover;
    object-fit: contain;
    "
    >
        <div class="innerCard">
            <h5 class="cardHeading text-center text-uppercase ">
                ${cObj.title}
            </h5>
        </div>
        <p class="cardDeatils">
        ${cObj.discription}
        </p>
    </div>

    `
    
})

document.getElementById("featuresCards").innerHTML = featCards; }

in console background image path is showing like this

background-image: url(". images firm3.jpg");

without forward slashes can anyone suggest me how I can fix this issue.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is a simple fix to an error caused by the double-quotes you are using in your background-image style:

style=
"
background-image: url("${cObj.imgPath}");
background-size: cover;
object-fit: contain;
"

Note the double-quote surrounding the style attribute that then contains another double-quote to surround the url of the image path.

Instead, switch the url quotes to singles (so url("...") becomes url('...')):

style=
"
background-image: url('${cObj.imgPath}');
background-size: cover;
object-fit: contain;
"

And this will work exactly as expected.

about 4 years ago · Juan Pablo Isaza Report

0

In fact you dont need the quotes for setting background url. can be set as :-

background: url(${cObj.imgPath});

let cardsObject = 
[
    {
        title:"FMCG-F&B", discription:"marketing and business planning",imgPath:"./images/firm3.jpeg"
    } 

];

(function cardShow() {
    let featCards =""; 
    cardsObject.forEach((cObj) => {
        
        featCards+=
        `
        <div class="card mr-2 mt-2"
        style=
        "
        background: url(${cObj.imgPath});
        background-size: cover;
        object-fit: contain;
        "
        >
            <div class="innerCard">
                <h5 class="cardHeading text-center text-uppercase ">
                    ${cObj.title}
                </h5>
            </div>
            <p class="cardDeatils">
            ${cObj.discription}
            </p>
        </div>

        `
        
    });

    document.getElementById("featuresCards").innerHTML = featCards; 
})();
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div id="featuresCards"></div>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!