So I am trying to make a string out of a string and a passed variable(which is a number).
How do I do that?
I have something like this:
function AddBorder(id){
document.getElementById('horseThumb_'+id).className='hand positionLeft'
}
So how do I get that ‘horseThumb’ and an id into one string?
I tried all the various options, I also googled and besides learning that I can insert a variable in string like this getElementById("horseThumb_{$id}") <– (didn’t work for me, I don’t know why) I found nothing useful. So any help would be very appreciated.
Your code is correct. Perhaps your problem is that you are not passing an ID to the
AddBorderfunction, or that an element with that ID does not exist. Or you might be running your function before the element in question is accessible through the browser’s DOM.Since ECMAScript 2015, you can also use template literals (aka template strings):
To identify the first case or determine the cause of the second case, add these as the first lines inside the function:
That will open pop-up windows each time the function is called, with the value of
idand the return value ofdocument.getElementById. If you getundefinedfor the ID number pop-up, you are not passing an argument to the function. If the ID does not exist, you would get your (incorrect?) ID number in the first pop-up but getnullin the second.The third case would happen if your web page looks like this, trying to run
AddBorderwhile the page is still loading:To fix this, put all the code that uses AddBorder inside an
onloadevent handler: