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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:28:42+00:00 2026-06-06T00:28:42+00:00

Can’t seem to get jQuery to change my css. In my css file this

  • 0

Can’t seem to get jQuery to change my css.

In my css file this works…

.closed {
    background-image: url(http://70.55.103.238/Images/menu-collapsed.gif);
}

I need to be able to change this dynamically with jQuery, so I am calling this in my $(document).ready(function()…

$('.closed').css({ 'background-image' : 'url(http://'+window.location.host+'/Images/menu-collapsed.gif)' }); 

If it’s not obvious, the reason I need to do this is that I cannot use a relative path to the images folder and the host is not a constant.

First question, why is the jquery call not working? If I put an alert like this…

alert('url(http://'+window.location.host+'/Images/menu-collapsed.gif)')

I can see that the URL is correct, and if I enter the url displayed in the alert directly into the address bar I do get the gif. So I am confident that the URL being passed is correct. I must assume that the css is not getting changed.

Second question, is there any way to dynamically get the host url in the .css file so that I do not have use jquery?

Thanks.

Update:

I tried the following just to make it simple..

CSS…

.closed {
    background-image: url(../Images/menu-collapsed.gif);
}

javascript…

$('.closed').css({ 'background-image' : 'url(../Images/menu-expanded.gif)' }); 

Note that the css is set to use menu-collapsed.gif and the javascript changes it to men-expanded.gif. But the image that is displayed is the menu-collapsed.gif.

  • 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-06T00:28:43+00:00Added an answer on June 6, 2026 at 12:28 am

    Ok I figure this out. I am not sure if this is by design, but I found that I have to reset the css any time I change a class controlled by jQuery css. In my particular case I was setting the image before I had any DOM elements with a class of .closed.

    What I actually have is a Tree Menu made up of ul li elements which is created in the $(document).ready(function() with all the nodes of the tree assigned the .closed class. I moved the $(‘.closed’).css() call so that it was executed right after I added the tree to the DOM. and it worked.

    The same holds true for the .open class I use for when a user clicks a closed ul. I had to put a $(‘.open’).css call right after I changed the class from .closed to .open. and again right after a change back to closed.

    So, if I understand what I am seeing $(‘.closed’).css must not be a persistant change to the css like manually typing it into the script. Am I interpreting this correclty?

    If any one is interested here is kind of what my jquery looks like now with the .css() calls where they are working…

    $(document).ready(function() {
        $.get('treeMenu.xml', function(d) {
    
            function makeMenu($xml) {
                var markup = "";
                function processXml() {
                    //CODE TO PARSE XML INTO MENU TREE IN MARKUP VAR
                }
                $xml.children().each(processXml);
                return markup;
            }
    
            tree = makeMenu($(d));
            $('div.treeMenu').append(tree);
            //HAD TO PUT THE CSS CALL HERE FOR IT TO WORK
            $('.closed').css({backgroundImage: "url(http://"+window.location.host+"/Images/menu-collapsed.gif)"});
    
            $('.treeMenu ul,li').each(function() {
                if ($(this).hasClass("leaf")) {
                    $(this).click(function() {
                        //CODE FOR CLICK ON LEAFS
                    }); 
                }else {
                    $(this).click(function() {
                        if ($(this).hasClass("closed")) {
                            $(this).removeClass("closed").addClass("open");
    
                            //HAVE TO PUT THIS HERE FOR EVERY CLICK TO GET A CHANGE IN THE GIF
                            $('.open').css({backgroundImage: "url(http://"+window.location.host+"/Images/menu-expanded.gif)"});
    
                    } else if ($(this).hasClass("open")) {
                            $(this).removeClass("open").addClass("closed");
    
                            //HAVE TO PUT THIS HERE FOR EVERY CLICK TO GET A CHANGE IN THE GIF
                            $('.closed').css({backgroundImage: "url(http://"+window.location.host+"/Images/menu-collapsed.gif)"});
    
                    } else {
                            $(this).addClass("open");
    
                            //HAVE TO PUT THIS HERE FOR EVERY CLICK TO GET A CHANGE IN THE GIF
                            $('.open').css({backgroundImage: "url(http://"+window.location.host+"/Images/menu-expanded.gif)"});
                     }  
                });
            }       
          });       
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can I make my own headers in HTTP request ? e.g. This is normal
Can't seem to get the Back Button to appear in a UINavigationController flow. I
Can this be done? It seems like this should be possible. In general, I
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
I have a jquery bug and I've been looking for hours now, I can't
Can I run this in a Windows command prompt like I can run it
Can anybody help me? What should be the datatype for this type -07:00:00 of
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can't figure out how to do this in a pretty way : I have
Can we change the color of the divider? Apple documentations says, that we can

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.