Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:32:34+00:00 2026-06-03T02:32:34+00:00

I have a javascript that is to be used to tag text in a

  • 0

I have a javascript that is to be used to tag text in a textarea when a user clicks a button. Right now I have only gotten it to work for one button. There are 8 buttons that need to be able to tag in total but I don’t want to repeat the same code over and over for each button id, so I tried a for loop but this didn’t work. I’m not quite sure why though.

These are the button ids: edit-button0, edit-button1, edit-button2, …,edit-button8

I’ve added an alert in the for loop to check the button id.

No matter which button I click later it says button id is edit-button8, and it adds the tag connected to that button.

Any ideas why this doesn’t work or how I should do it instead.

/*globals document*/
(function ($) {
    "use strict";
    $(document).ready(function () {

            for (i=0;i<8;i++) {

                $("#edit-button"+i).click(function () {
                        alert("#edit-button"+i);
                        var tag = $("#edit-button"+i).attr("value");
                        var id = "protocol"; /* id of textarea */

                        var element  = document.getElementById(id); /* HTML element object */
                        var start    = element.selectionStart;      /* start pos of selection */
                        var end      = element.selectionEnd;        /* end pos of selection */
                        var text     = element.value;           /* whole text */

                        var prefix   = text.substring(0, start);    /* part before selection */
                        var selected = text.substring(start, end);  /* selected text */
                        var suffix   = text.substring(end);         /* part after selection */
                        /* insert tags in selection */
                        selected     = "["+tag+"]" + selected + "[/"+tag+"]";

                        /* update HTML object */
                        element.value      = prefix + selected + suffix; /* selected text */

                        element.selectionStart = start;                      /* new start pos */
                        element.selectionEnd   = start + selected.length;    /* new end pos */


                        return false;
                    });
            }
    });
})(jQuery);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:32:36+00:00Added an answer on June 3, 2026 at 2:32 am

    You could give all the buttons a class .tagButtons and then use .each() in jQuery to loop over them like this

    $('.tagButtons').each(function(){ 
         $(this).click(function(){
    
    
                 var tag = $(this).attr("value");
                 var id = "protocol"; /* id of textarea */  
    
                 var element  = document.getElementById(id);                         
                 var start    = element.selectionStart;     
                 var end      = element.selectionEnd;       
                 var text     = element.value;                                             
                 var prefix   = text.substring(0, start);   
                 var selected = text.substring(start, end);                             
                 var suffix   = text.substring(end);    
                 selected     = "["+tag+"]" + selected + "[/"+tag+"]";                                                    
    
                 element.value      = prefix + selected + suffix;  
    
                 element.selectionStart = start;                                   
                 element.selectionEnd   = start + selected.length;    
    
                 return false;
    
         });
    }); 
    

    EDIT –

    As an alternative and to help in the future, i think the issue with your code was your for loop. Have a look at this jsFiddle example to show how the loop should look.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one javascript function that used to display the menu and content in
I have a lengthy JavaScript file that passes JSLint except for used before it
I have JavaScript that is doing activity periodically. When the user is not looking
I have a javascript that displays a generated text into a div: document.getElementById('phrase').innerHTML =
I've seen this tag used after javascript functions for over a decade now and
I have a javascript file that is normally used in a web browser using
BEFORE CHAOS I used to have this one... at the head part <script type=text/javascript>$(function()
I have Javascript that opens another window and registers a click handler for all
I have some javascript that goes out and fetches a javascript class on another
I have a javascript that calls a function each 1 minute. That function sends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.