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 5973331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:47:08+00:00 2026-05-22T20:47:08+00:00

edit: This question is about jQuery refactoring. Basically I have a big block of

  • 0

edit: This question is about jQuery refactoring. Basically I have a big block of code but I want to see if other folks can think of a better way to refactor it. Since I’m new to jQuery I’m not sure if I’m on the right track or not with my design

original post:
I’m working on a bookmarklet which adds HTML elements to the page. I’m building a sidebar which is a div with a ul inside of it. I’m trying to keep my styles separate from my code and to also write things so that they will be easy to manage if I need to make changes in the future. Is it possible to refactor this code to make it cleaner/more efficient?

myObj = {
$sidebar: {},   
createSidebar: function () {
    var self = this, $undo = {}, $redo = {}, $email = {}, $reset = {}

    self.$sidebar = $("<div id='myObj-sidebar'><ul></ul></div>");

    $undo = $('<a></a>', {
        id: 'sidebar-undo',
        className: 'sidebar-item',
        href: '#',
        text: 'Undo',
        click: function (e) {
            //self.doUndo();
            e.preventDefault();
        }
    });

    $redo = $('<a></a>', {
        id: 'sidebar-redo',
        className: 'sidebar-item',
        href: '#',
        text: 'Redo',
        click: function (e) {
            //self.doRedo();
            e.preventDefault();
        }
    });

    $email = $('<a></a>', {
        id: 'sidebar-change-email',
        className: 'sidebar-item',
        href: '#',
        text: 'Change E-Mail',
        click: function (e) {
            //self.createEmailDialog();
            e.preventDefault(); 
        }
    });

    $reset = $('<a></a>', {
        id: 'sidebar-reset-all',
        className: 'sidebar-item',
        href: '#',
        text: 'Reset All',
        click: function (e) {
            //self.doReset();
            e.preventDefault();
        }
    });

    self.$sidebar.find('ul').append($undo, $redo, $email, $reset);
    self.$sidebar.find('.sidebar-item').wrap('<li/>');
    self.$sidebar.appendTo("body");
}
}
  • 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-05-22T20:47:08+00:00Added an answer on May 22, 2026 at 8:47 pm

    I’m not really sure what the question is – my interpretation is that you’re asking for refactoring advice. If that’s what you’re looking for, here’s the way I’d probably implement the same requirements:

    jQuery('<div><ul></ul></div>')
      .appendTo("body")
      .css({
        background: "white",
        left: 0,
        position: "absolute",
        textAlign: "left",
        top: '50%'
      })
      .find('ul')
        .css({
          listStyleType: 'none',
          margin: 0,
          padding: 0
        })
        .append('<li><a href="" class="sidebar-undo">Undo</a></li>')
        .find('.sidebar-undo')
          .click(function(e){
            e.preventDefault();
            // your 'undo' code
          })
          .end()
        .append('<li><a href="" class="sidebar-redo">Redo</a></li>')
        .find('.sidebar-redo')
          .click(function(e){
            e.preventDefault();
            // your 'redo' code
          })
          .end()
        .append('<li><a href="" class="sidebar-changeEmail">Change E-Mail</a></li>')
        .find('.sidebar-changeEmail')
          .click(function(e){
            e.preventDefault();
            // your 'changeEmail' code
          })
          .end()
        .append('<li><a href="" class="sidebar-doReset">Reset All</a></li>')
        .find('.sidebar-doReset')
          .click(function(e){
            e.preventDefault();
            // your doReset code
          })
          .end()
        .find('li')
          .css({
            padding: '0.5em'
          })
          .end()
        .find('a')
          .css({
            background: "whatever",
            color: "etc"
          });
    

    Notes:

    • e.preventDefault() should generally be the first line of your handler, that way if an exception is thrown later, the anchor will still not navigate (easier to debug);
    • you don’t need to give any of your elements IDs, since you have direct DOM handles on everything already;
    • your hrefs should ideally be useful, taking the user to a legitimate page if they right-click and do “open in new tab” (using “preventDefault” will keep them from navigating away on regular left-click);
    • since it’s a bookmarklet, don’t worry too much about separating out your styles – you’re using jQuery, so you can easily grab the elements you care about and apply CSS properties directly;
    • the example above has no variables aside from the e event object – to me, this is a more elegant solution than creating a self reference and a bunch of collection objects (that’s just my opinion).

    Hope this helps!

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

Sidebar

Related Questions

I have a potentially strange question about this and jQuery plugins As I understand
EDIT: This question is about finding definitive reference to MySQL syntax on SELECT modifying
EDIT: This question is more about language engineering than C++ itself. I used C++
(EDIT: This question is now outdated for my particular issue, as Google Code supports
I've read some of the previous question and answers about this issue but couldn't
I have a really basic question about jquery. However I don't know how to
I just read this question about how to fix something in jQuery 1.2.6 and
Earlier, I answered this question , which was basically about removing a table row.
I have a newbie question about jQuery. Why is it that I can trigger
Edit: This question was written in 2008, which was like 3 internet ages ago.

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.