How to dynamically change the name of an element?
-
Hello!
The site template generates several blocks using the code:
echo '& lt; ul id = "menu" & gt;';
as a result, several blocks with the same ID are formed on the page.
Question:
How to dynamically substitute a prefix for this ID so that each subsequent block on the page is formed with the ID:
menu_1
menu_2
menu_3
etc., as this code is called?
Thank you in advance!JavaScript Eleanor Cooke, Feb 21, 2020 -
And if not in a loop, then like this:
$menuId = 0; // Где-то в области видимости объявляем переменную.
// Потом просто
echo '<ul id="menu_'.++$menuId.'">';
In general, it is better to write HTML and PHP separately. I mean, don't write all HTML in templates in PHP strings.William House -
If blocks are formed in a loop, then you can add the iteration number to the ID by simple concatenationAnonymous
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!