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

  • Home
  • SEARCH
  • 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 7914119
In Process

The Archive Base Latest Questions

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

Yes, I understand that JavaScript doesn’t have classes per se, so I’ll just throw

  • 0

Yes, I understand that JavaScript doesn’t have “classes” per se, so I’ll just throw that out there up front and move on.

I have an app that has a left sidebar that lists a bunch of tags. I’m manipulating these tags via JavaScript and AJAX to add, remove, and update the tags.

When I currently manipulate the tags, it goes something to the effect of (note this code is a dumbed-down version of what I’m doing, but conveys the point; also, I’m using the jQuery library):

# HTML page...
<div id="tags">
  <div class="tag" id="tag_1">tag text</div>
  <div class="tag" id="tag_2">tag text</div>
  <div class="tag" id="tag_3">tag text</div>
  ...
</div>

# Javascript to create new tag
$("#tags").append("<div class='tag' id='tag_15'>text</div>")

# update a tag
$("#tag_10").replace("<div class='tag' id='tag_10'>updated tag</div>")

# remove a tag
$("#tag_10").remove()

As a side note, I’m using the Rails framework and some of the JS is created on the server via js.erb files and sent back to the client to evaluate. I’m aware of some concerns with servers generating JS, but it’s OK in this situation.

So I decided that I should group this functionality into a JavaScript “class” that controls the tags on my page.

The issue is that I have a single tags sidebar, so I don’t need multiple Tag objects…I just need a bunch of named methods that allow me to manipulate the tags on my page in an orderly fashion.

I would like to be able to do things like:

Tag.append({ text: "tag text", id: <id of tag> }) => the append functionality above
Tag.update(<id to update>, { <hash of options> })
Tag.remove(<id of tag to remove>)

I don’t understand how I would achieve this functionality with JavaScript.

My thoughts are doing something like:

# "class" definition
function Tag() {
  var methods = {
    init : function() { ... }

    append : function(options) {
      var element = <...build DIV element...>;
      $("#tags").append(element);
    }

    remove : function(domid) {
      $(domid).remove();
    }
  }
}

# usage
var tag = Tag.new()
tag.remove("tag_10")
...

However, if I do it this way, will I have to create a new var tag = Tag.new() each time I want to reference the Tag class (since I’m sending some code back via the server)?

  • 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-03T14:00:36+00:00Added an answer on June 3, 2026 at 2:00 pm

    Another option is to group your functions as a jQuery plugin, returning an object with chainable utility functions:

    $.fn.tags = function() {
      var tagContainer = this;
      var tags = {
        append : function(options) {
          var element = <...build DIV element...>;
          tagContainer.append(element);
          return tags;
        },
        replace: function () {
          <...do stuffs here...>
          return tags;
        },
        remove : function(domid) {
          $(domid, tagContainer).remove();
          return tags;
        }
      }
      return tags;
    }
    
    $('#tags').tags().append({text: 'foo', id: 'bar'}).remove('#tag_10');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that the answer is yes - but..., and I'm just trying to
I understand that floating point calculations have accuracy issues and there are plenty of
Yes, I know you have to embed the google analytics javascript into your page.
Yes, I know, the archive bit is evil. That being said, is there support
I understand that this problem is incredibly common and I have read through quite
Yes i have been through the other questions that are related to this, but
(Yes, I understand that the whole point of dylib is that it loads dynamically,
I understand that const_cast to remove constness of objects is bad, I have the
I understand that web containers like jboss have a parameter to mention the number
I have this javascript function I am trying to understand. I don't know if

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.