How to dynamically change the name of an element?

  • 0

    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.


    How to dynamically substitute a prefix for this ID so that each subsequent block on the page is formed with the ID:


    etc., as this code is called?

    Thank you in advance!
    JavaScript Eleanor Cooke, Feb 21, 2020

  • 2 Answers
  • 0
    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

  • 0
    If blocks are formed in a loop, then you can add the iteration number to the ID by simple concatenation

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!