Please tell me how when you click on the button assign a unique id in the html tag

<p id="">Текст</p> 

Source

 <textarea readonly id="txt" type="text" name="area" style="width:100%; height:300px; "> <?php echo file_get_contents('textfile.txt') ?> </textarea> <br> <input class="btn btn-success mr-2" id="tx2" type="button" onclick='document.querySelector("textarea[name=area]").value+="<p class=\"dragElement txt\">" + document.getElementById("tx").value + "</p>"' value="Вставить текст"> &nbsp; <input id="tx" type="text" placeholder="Введите ваш текст"> 
  • To assign a unique id - you must uniquely identify this tag - have ideas how you want to do this? - Axenow
  • @Axenow must be assigned an id for each tag. <p id = "<? php $ a;?> Text </ p> It would not be bad to assign the value of the variable $ a. The value of the variable is time in ms. - Vladislav Samokhin
  • For each tag on the page? That is, you first generate the page, then click on the button and each tag gets a unique id - right? - Axenow
  • At the moment, when I click on the button, I insert it into the text in the textarea. Insert text button - inserts <p> Text </ p> - Vladislav Samokhin
  • And you can ask to add here your which, you have? Based on it, it will be easier to do what you ask for. - Axenow

1 answer 1

 document.getElementById("tx2").onclick = function(){ var timeStampInMs = Date.now(); document.getElementById("txt").value += "<p class=\"dragElement txt\" id=\""+timeStampInMs+"\">" + document.getElementById("tx").value + "</p>" } 
 <textarea readonly id="txt" type="text" name="area" style="width:100%; height:300px; "> <?php echo file_get_contents('textfile.txt') ?> </textarea> <br> <input class="btn btn-success mr-2" id="tx2" type="button" value="Вставить текст"> &nbsp; <input id="tx" type="text" placeholder="Введите ваш текст"> 

  • It produces an error on the HTTP ERROR 500 page - Vladislav Samokhin
  • And you just took and copied? The upper part (first window) is javascript. Put it in <script> </ script>. The bottom is your Html. Replace with what you provided. - EatMyDust
  • Yes, everything turned out, thank you very much. - Vladislav Samokhin