It is necessary to add, and not to write it again. And delete exactly certain text, but not all at once. Thank!

  • When you click on the button to get the text, cut a certain line from it and add the input value to the current value? - Dmitry B.
  • Yes, but how to cut ODA. a string? - Pro100Paket

2 answers 2

var input = document.getElementById('myInput'); var btn = document.getElementById('myButton'); btn.onclick = function() { input.value += 'click! '; } 
 <input id="myInput" /> <input id="myButton" type="button" value="click me!"> 

Try this option.

  • and remove (ONLY DEFINED PART OF THE TEXT) how? - Pro100Paket
  • and yes, btn.onclick = ... gives an error. Fixed this: $ ('# myButton'). Click (function () {...}); But the main thing - inpt.value is also a mistake! - Pro100Paket
  • @ArtemPro, try again. I changed the answer. Press the button and see what happens - Chloroform
  • @ArtemPro, for removal of part of the text, read the documentation - Chloroform
  • is it possible in more detail? I did not find what I need in the documentation .. - Pro100Paket

 var $text = document.querySelector('#text'), $buttonAdd = document.querySelector('#button-add'), $buttonRemove = document.querySelector('#button-remove'), defaultText = $text.value; // значение текстового поля на момент загрузки $buttonAdd.addEventListener('click', function(){ $text.value = defaultText + 'Additional text'; }); $buttonRemove.addEventListener('click', function(){ $text.value = defaultText; }); 
 <input type="text" name="text" id="text" value="Some text..."> <button id="button-add">Add text</button> <button id="button-remove">Remove text</button> 

  • There is no jquery in the tags, and your version will not work with the entered text. - Artem Gorlachev
  • First, where in the answer do you see jQuery usage? Secondly, the question does not say anything about the entered text. Written именно определённый текст . What is this text? Who defined? Unclear. Thirdly, your "minus" is meaningless, my answer is fully adequate to the question - vihtor
  • Moreover, I commented on the code in the place where the value is determined . In order for you to pay attention to this moment and understand how this text is defined in my case. If we are talking about the entered text, then you need to designate it in the question - vihtor
  • I'm sorry, your syntax confused me. but in the case of input, it seems obvious to me that the text may change. - Artem Gorlachev
  • No, not obvious. Your question is very abstract. And even if it were obvious, it is not clear what text to add and to what? To what the user entered? And what to delete? What if the user has already deleted some text? Maybe you will give a specific example of what should happen and how? - vihtor