Solution 1 :

I guess you want to add an img element image after your innerHTML text.

If that is the case,

var Bearing = document.getElementById("rptAracDetay_Label28_0");                    
if (Bearing.innerHTML.replace(':   ', '') == '0') { 
    Bearing.innerHTML = ':   North'; 
    var img = document.createElement('img'); 
    img.src = 'https://xyz/x.png'; 
    Bearing.appendChild(img); 
}
else if (Bearing.innerHTML.replace(':   ', '') == '180') {
    Bearing.innerHTML = ':   South'; 
    var img = document.createElement('img'); 
    img.src = 'https://xyz/y.png'; 
    Bearing.appendChild(img); 
}

Upvote if you find it useful.

Solution 2 :

I think your if/else portion is being executed before actually Bearing variable initiated. So use a if condition to check if Bearing variable is actually initiated or not like:

if(Bearing){
    //do your stuff;
}

Solution 3 :

Not sure what you are trying to achieve, but I would say

const Bearing = document.getElementById("rptAracDetay_Label28_0");
                    
  Bearing.innerHTML.replace(':   ', '') === '0' ?
                     Bearing.innerHTML = ':   North' : Bearing.innerHTML = ':   South';

Also, is '0' and '180' a string or a number? Does Bearing needs to be capital for some reason?

Problem :

The below Js Code is working. I want to add a picture here. If the value is 0 then x.png is 180 then y png.

how can I do that. I will be glad if you help.
Thank you

var Bearing = document.getElementById("rptAracDetay_Label28_0");
if (Bearing.innerHTML.replace(':   ', '') == '0') { 
    document.getElementById("rptAracDetay_Label28_0").innerHTML = ':   North';
}
else if (Bearing.innerHTML.replace(':   ', '') == '180') { 
    document.getElementById("rptAracDetay_Label28_0").innerHTML = ':   South';
}

Comments

Comment posted by Lain

There is no need in refetching the element inside the conditions.

Comment posted by Erhan Şimşek

no background picture. I want to add an image to the end of the line immediately.

Comment posted by Juhil Kamothi

End of line means under the same element with id “rptAracDetay_Label28_0” right? and you want to add img HTML element. correct me if I am wrong.

Comment posted by Juhil Kamothi

@ErhanŞimşek please check the edited answer.

Comment posted by Erhan Şimşek

command works no problem. I just want it to add a picture according to the values I gave to the end of the line. no background

Comment posted by Erhan Şimşek

command works no problem. I just want it to add a picture according to the values I gave to the end of the line. no background

By