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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:50:33+00:00 2026-06-13T06:50:33+00:00

I have to access CSS class by name, and the code below works. However

  • 0

I have to access CSS class by name, and the code below works. However if I try hui["myclass"] or hui[".myclass"] instead of hui[0] it cannot find it.

function change_class() {
  var hui = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
  hui[0].style["color"] = "#ff0000"
}
.myclass {
  color: #00aa00;
}
<div class="myclass" onclick="change_class()">Some Text</div>

EDIT: I need to be able to acess as i described on line 1 i dont want to acess trough individual elements on page, but directly to stylesheet by class name.

So you all saying i cannot access to stylesheet by class name only by index?

  • 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-13T06:50:34+00:00Added an answer on June 13, 2026 at 6:50 am

    No, you can’t access them by the selector – it’s a simple list. You first had to build an index for it:

    // assuming those are the right rules (ie from the right stylesheet)
    var hui = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
    
    var styleBySelector = {};
    for (var i=0; i<hui.length; i++)
        styleBySelector[hui[i].selectorText] = hui[i].style;
    
    // now access the StyleDeclaration directly:
    styleBySelector[".myclass"].color = "#ff0000";
    

    Of course this is not a fool-proof method, there could be

    • multiple selectors like .myClass, .myOtherClass
    • multiple occurences of one selector (though it doesn’t matter, the last declaration overwrites previous styles anyway)

    and instead of blindly assigning the color property you first should check for existence of the declaration.

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

Sidebar

Related Questions

i have this code: $('.access a').toggle(function() { $('link').attr('href', 'styles/accessstyles.css'); $('body').css('font-size', '16px'); }, function() {
Modifying a SnapPages theme, meaning I only have access to the CSS stylesheet. I
In my app i have access to web server. It works fine in Some
I would like the user to have limited access to controlling certain CSS attributes
Say I have a .box class in CSS: .box { height: 50px; } Now,
I want for all images on my webpage to have CSS class fadeIn so
I have a Soft.CSS file which has following class .check_out { width:58px; background-image:url(/a/i/softadd/checkout.gif);} What
I have a div with Class. For example <div class=button check></div> .The css is
Update: I have tried Sheldon's code below but I have struck a couple of
I have form in a ecommerce solution. I dont have access to the code

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.