0

I have a function that determines which source the image will be:

function facilityImg(arr, x) {
    switch (arr[x]) {
        case 'Yes':
            return "Images/checked.png";
        case 'No':
            return "Images/unchecked.png";
    }
}

Where arr is an array of Yes/No strings, and x is the index of each value.

I am trying to run this function like this:

let html = `<div class="facilityCell">
               <img class="facilityIcon" title="Air-Conditioner" src="Images/air-conditioner.png">
               <img class="facilityIcon" src='facilityImg(${facilityArr}, 0)'>
            </div>`

When inspecting the html element, I get this as the src:

Output of the img src

Is there something I am doing wrong? I have tried with [src] and :src which should apparently work. And before you shoot me down in the comments, I am repeating this code but slightly different each time for a minor project - cant be bothered to write out another 400 lines in the correct way.

Is what I am wanting to do possible?

3
  • 1
    Try this src="${facilityImg(facilityArr, 0)}" Commented Jul 29, 2021 at 9:47
  • @AHMEDSAJJAD kindly post it as an answer so can mark it as an answer, as it can help someone in future. Commented Jul 29, 2021 at 9:53
  • Posted it as an answer. @Elliot Cullen please accept it as the answer Commented Jul 29, 2021 at 9:53

1 Answer 1

1

You just need to correct the interpolation of your function. Try

let html = `<div class="facilityCell">
              <img class="facilityIcon" title="Air-Conditioner" src="Images/air-conditioner.png">
              <img class="facilityIcon" src="${facilityImg(facilityArr, 0)}">
            </div>`

Hope that fixes it

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

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.