'i want to Set text-cursor to specific word(position given in below code) in a textarea?

My target is to click button and cursor will pop up on word assinged to given button. I have tried everything i can available online. but nothing worked. I'm in initial phase of learning javascript. I'm working on blog project and i need to insert extra code at this given postions thats why cursor need to set there. It will save time.

<script type="text/javascript">
function StringSearch(clicked_id) 
{
    if (clicked_id == 'top6-button-1') {
        var SearchTerm = 'item-1-end' ;
  //  block of code to be executed if condition1 is true
} else if (clicked_id == 'top6-button-2') {
    var SearchTerm = 'item-2-end' ;
  //  block of code to be executed if the condition1 is false and condition2 is true
} else if (clicked_id == 'top6-button-3') {
    var SearchTerm = 'item-3-end' ;
  //  block of code to be executed if the condition1 is false and condition2 is false
} else if (clicked_id == 'top6-button-4') {
    var SearchTerm = 'item-4-end' ;
    
} else if (clicked_id == 'top6-button-5') {
    var SearchTerm = 'item-5-end' ;
    
} else if (clicked_id == 'top6-button-6') {
    var SearchTerm = 'item-6-end' ;
    
} else {
    var SearchTerm = 'comparing-list-end' ;
}
    
    var TextSearch = document.getElementById("top6-initial-code").value;

     if (SearchTerm.length > 0 && TextSearch.indexOf(SearchTerm) > -1) {
         TextSearch.value = TextSearch.value + "\n";
  TextSearch.setSelectionRange(TextSearch.value.length, TextSearch.value.length);
  TextSearch.focus();
    
  } else {
    alert("No Data found in Text Area");
  }
}
</script>
<textarea name="top6-initial-code" id="top6-initial-code" cols="165" rows="40" value="all words i want to put cursor there in textbox, its too big">
<button type="button" id="top6-button-1" onclick="StringSearch(this.id)">Item 1</button>
<button type="button" id="top6-button-2" onclick="StringSearch(this.id)">Item 2</button>
<button type="button" id="top6-button-3" onclick="StringSearch(this.id)">Item 3</button>
<button type="button" id="top6-button-4" onclick="StringSearch(this.id)">Item 4</button>
<button type="button" id="top6-button-5" onclick="StringSearch(this.id)">Item 5</button>
<button type="button" id="top6-button-6" onclick="StringSearch(this.id)">Item 6</button>
<button type="button" id="top6-button-7" onclick="StringSearch(this.id)">Comparison</button>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source