# Modifying a Selection

**URL:** <https://discourse.mozilla.org/t/modifying-a-selection/30928>\
**Category:** Add-ons\
**Created:** [August 13, 2018, 11:28pm UTC](https://discourse.mozilla.org/t/modifying-a-selection/30928 "2018-08-13T23:28:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![msaroff](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/msaroff/32/20303_2.png) [@msaroff](https://discourse.mozilla.org/u/msaroff)\
**Post date:** [August 13, 2018, 11:28pm UTC](https://discourse.mozilla.org/t/modifying-a-selection/30928/1 "2018-08-13T23:28:28Z")

</div>

I am attempting to write a context menu that will apply tags to selected text.

I have the whole menu structure thing down, and based on some coding observations, this should work, it’s very proof of context now, but it does not.

I have this function:  
function getSel() // javascript  
{

```
    // obtain the object reference for the textarea>
    var txtarea = document.getElementById("mytextarea");
    // obtain the index of the first selected character
    var start = txtarea.selectionStart;
    // obtain the index of the last selected character
    var finish = txtarea.selectionEnd;
    //obtain all Text
    var allText = txtarea.value;
    // obtain the selected text
    var sel = allText.substring(start, finish);
    //append the text;
    var newText=allText.substring(0, start)+"[b]"+sel+"[/b]"+allText.substring(finish, allText.length);
        txtarea.value=newText;
    // do something with the selected content
}

```

This should apply the and tags to the selected text.

It is invoked by this menu selection: (other test menu functions omitted)

```
browser.menus.onClicked.addListener((info, tab) => {
    switch (info.menuItemId) {
.
.
.
        case "bbCode_Bold":
            getSel();
            break;

```

Why is this not tagging the selected text?

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [August 13, 2018, 11:33pm UTC](https://discourse.mozilla.org/t/modifying-a-selection/30928/2 "2018-08-13T23:33:57Z")

</div>

The problem here is that the background page is a different HTML context than the page that the context menu is triggered in. To change the page you need to use a content script.

Further, to find out why something is not working, I can really recommend [https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Debugging](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Debugging)
