You can insert your entire HTML inside of a backtick string and interpolate values like this:
let popupContent =
`<div id= "divTrophy">
<img src=${trophy}></img>
<button onClick="trophyCollection(${trophy})">Collect trophy</button>
<button onclick="closePopUp()">Close pop up</button>
</div>`
Answer from Alex Okarkau on Stack OverflowYou can insert your entire HTML inside of a backtick string and interpolate values like this:
let popupContent =
`<div id= "divTrophy">
<img src=${trophy}></img>
<button onClick="trophyCollection(${trophy})">Collect trophy</button>
<button onclick="closePopUp()">Close pop up</button>
</div>`
Try using string interpolation. Something like this.
`your_html_string ${your_dynamic_value} your_html_string`
You can find more info on interpolation here. How to interpolate variables in strings in JavaScript, without concatenation?
Videos
Your code is correct. Perhaps your problem is that you are not passing an ID to the AddBorder function, 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):
document.getElementById(`horseThumb_${id}`).className = "hand positionLeft";
To identify the first case or determine the cause of the second case, add these as the first lines inside the function:
alert('ID number: ' + id);
alert('Return value of gEBI: ' + document.getElementById('horseThumb_' + id));
That will open pop-up windows each time the function is called, with the value of id and the return value of document.getElementById. If you get undefined for 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 get null in the second.
The third case would happen if your web page looks like this, trying to run AddBorder while the page is still loading:
<head>
<title>My Web Page</title>
<script>
function AddBorder(id) {
...
}
AddBorder(42); // Won't work; the page hasn't completely loaded yet!
</script>
</head>
To fix this, put all the code that uses AddBorder inside an onload event handler:
// Can only have one of these per page
window.onload = function() {
...
AddBorder(42);
...
}
// Or can have any number of these on a page
function doWhatever() {
...
AddBorder(42);
...
}
if(window.addEventListener) window.addEventListener('load', doWhatever, false);
else window.attachEvent('onload', doWhatever);
In javascript the "+" operator is used to add numbers or to concatenate strings. if one of the operands is a string "+" concatenates, and if it is only numbers it adds them.
example:
1+2+3 == 6
"1"+2+3 == "123"
String concatenation
<?php
$rebate_no = 2;
echo "<table id='blacklistgrid_".$rebate_no."' class='table table-bordered table-hover table-striped blacklistgrid'>
<tr id='reb".$rebate_no_1."'>
<td>
<div class='btn-group'>
<select name='product_id_".$rebate_no[1]." id='product_id_".$rebate_no_1."' class='form-control prod_list'>
<option value='1'>Alabama</option>
<option value='2'>Alaska</option>
<option value='3'>Arizona</option>
<option value='4'>Arkansas</option>
<option value='5'>California</option>
</select>
</div>
</td>
</tr>
</table>";
?>
OR If you want to use the Zend specification
<?php
$rebate_no = 2;
echo "<table id='blacklistgrid_{$rebate_no}' class='table table-bordered table-hover table-striped blacklistgrid'>
<tr id='reb{$rebate_no_1}'>
<td>
<div class='btn-group'>
<select name='product_id_{$rebate_no[1]} id='product_id_{$rebate_no_1}' class='form-control prod_list'>
<option value='1'>Alabama</option>
<option value='2'>Alaska</option>
<option value='3'>Arizona</option>
<option value='4'>Arkansas</option>
<option value='5'>California</option>
</select>
</div>
</td>
</tr>
</table>";
?>
You started with double quotes, so you can do it this way:
<tr id='reb".$rebate_no."_1'>
or use curly braces {} around the variable:
<tr id='reb{$rebate_no}_1'>
Both are valid.
" + yourName + ",
"); document.write("You have been alive for " + ageInSeconds + " seconds
");No, there isn't. HTML is markup, it is not turing complete.
One (primitive) way to achieve this with JavaScript would be
<a href="#"
onclick="window.location.hash='#'+document.getElementsByTagName('div')[0].id; return false;">
go to the 1st DIV tag.
</a>
But since those links are useless when JS is not available, they should probably only be generated by JS in the first place.