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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:37:11+00:00 2026-06-16T04:37:11+00:00

I am trying to simplify (or improve) my codes in javascript. I have task.prototype.init

  • 0

I am trying to simplify (or improve) my codes in javascript.

I have

task.prototype.init = function (){

      //for loop and create bunch of links
            var size=document.createElement('a');
                size.innerHTML='test'
                size.id=value;
                size.onclick=this.changeSize.bind(this);

                body.appendChild(size);
}


task.prototype.changeSize = function(e){
  for(var i=0; i<e.target.parentNode.childNodes.length; i++){
   e.target.parentNode.childNodes[i].style.backgroundColor='white';
  }
 e.target.style.backgroundColor='red';
 return false;
}

my html is like

<div>
   <a href='#' id='1'>test</a>
   <a href='#' id='2'>test</a>
   <a href='#' id='3'>test</a>
<div>

My codes will change the background color of all the <a> links to white and give selected <a> tag a red background.
It suits what I need but I got a feeling it can be prettier in my changeSize function.

Are there better way to write it? Thanks a lot!

  • 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-16T04:37:12+00:00Added an answer on June 16, 2026 at 4:37 am

    Use variables to avoid spaghetti code.

    task.prototype.changeSize = function(e) {
        var target = e.target,
            nodes = e.target.parentNode.childNodes, node;
    
        for (var i = nodes.length, node = nodes[i]; i--;) {
            node.style.backgroundColor = 'white';
        }
    
        target.style.backgroundColor = 'red';
        return false;
    };
    

    Maybe even?:

    task.prototype.changeSize = function(e) {
        var target = e.target,
            nodes = e.target.parentNode.childNodes, 
            i = nodes.length, node;
    
        for (node = nodes[i]; i-- && node.style.backgroundColor = 'white';);
    
        return !(target.style.backgroundColor = 'red');
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom HTML Helper to help simplify my masterpages menu,
For 3 full days, I have been trying to improve the performance of my
Am trying to simplify the create / edit view for a multi-model controller. User
I am trying to simplify some of the bookmarklets that I have created over
I'm trying to simplify some mysql. I have three selects and three tables: SELECT
I'm trying to simplify a contrived callback function in PHP for sorting. function my_sort($a,
I'm trying to simplify a bunch of header file include spaghetti by using forward
I've been trying to figure out how to create links between different tables and
I'm trying to simplify routes. What I have now still requires a reference inside
I am trying to simplify the following codes. The codes seem to redundant to

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.