Solved

jquery getting overwritten by CSS?

Posted on 2011-09-12
13
327 Views
Last Modified: 2012-06-21
I think my jQuery CSS changes are getting overwritten by the THEME css for the page that gets applied.

How can I correct this?

I either want to run the jquery AFTER the css gets applied

or

set up a trigger event on the css getting changed.  If the class changes to this then do something.

        $('span[id*="TopCart2_lblTotalItems"]').css("color", "#532B8F");
        $('span[id*="TopCart2_Label1"]').css("color", "#532B8F");
        $('span[id*="TopCart2_lblTotalAmount"]').css("color", "#532B8F");

In essence, if the color changes to anything OTHER THAN #532B8F then I want to change the color BACK to #532B8F



0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 167 total points
ID: 36524911
>I either want to run the jquery AFTER the css gets applied

most of time it's the case
Check your selectors with an alert for example :

alert( $('span[id*="TopCart2_lblTotalItems"]').length ); // should be greater than zero
alert( $('span[id*="TopCart2_Label1"]') ); // should be greater than zero
alert( $('span[id*="TopCart2_lblTotalAmount"]') ); // should be greater than zero
$('span[id*="TopCart2_lblTotalItems"]').css("color", "#532B8F");
$('span[id*="TopCart2_Label1"]').css("color", "#532B8F");
$('span[id*="TopCart2_lblTotalAmount"]').css("color", "#532B8F");
0
 
LVL 13

Assisted Solution

by:haloexpertsexchange
haloexpertsexchange earned 167 total points
ID: 36524948
what you could do would be to add the css with a new attribute of style and then add the css to that, but what happens when you use jquery with the .css() it adds a style attribute to the elements that it is effecting.
So according to the normal order of precedence for css anything you add with jquery should always be what you get since it first looks for external css files and then applies those in the order that you include them on the page, then any internal css blocks in the head and then the css that is on an element level in the style attribute on the element. So what ever is in that style element should apply even if the external them styles want something else to happen/
0
 
LVL 18

Assisted Solution

by:Sudaraka Wijesinghe
Sudaraka Wijesinghe earned 166 total points
ID: 36524962
It's possible that your CSS selectors are matching the elements more closely than the jQuery selectors, that's the first thing to check.
Then check if the CSS styles are using the !important flag.

You could try adding the !important flag to your jQuery, unfortunately .css() doesn't support this so you will have to do something like this.
$('span[id*="TopCart2_lblTotalItems"]').attr("style", "color:#532B8F !important");

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36524969
They were all equal to 0.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36524985
Are your waiting the total loading of the page with the following for example :

$(document).ready(function() {

alert( $('span[id*="TopCart2_lblTotalItems"]').length ); // should be greater than zero
alert( $('span[id*="TopCart2_Label1"]') ); // should be greater than zero
alert( $('span[id*="TopCart2_lblTotalAmount"]') ); // should be greater than zero
$('span[id*="TopCart2_lblTotalItems"]').css("color", "#532B8F");
$('span[id*="TopCart2_Label1"]').css("color", "#532B8F");
$('span[id*="TopCart2_lblTotalAmount"]').css("color", "#532B8F");


})
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36524994
>>>>Are your waiting the total loading of the page with the following for example :

$(document).ready(function() {

--------------------------------------------------


Yes
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36525015
Ok, so double check the ID of the SPAN

If you can use Control.ClientID : http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

http://dotnetslackers.com/ASP_NET/re-113573_The_difference_between_ID_ClientID_and_UniqueID.aspx

$('span[id*="TopCart2_lblTotalItems"]')

mean something like : <span id="smlfksmfsflksflmsfmskTopCart2_lblTotalItemsfksflfmkfmskj">
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36525082
It seems to happen on the button click...

see screencast knowlton-499411.flv
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36525095
At about time index :48 I click on the "Add to Cart" button and the cart styling goes all crappy.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36525142
If I refresh the page AFTER the styling goes away....the styling gets applied again.

It's something with the button click.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36525147
with << code behind >>
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36525269
Now I can't get it to do it anymore....

:::: frustrating :::::


I guess it was a temporary glitchy thing....oh well.

Thanks for the time and effort...if it happens again I'll come back to try a few of the other suggestions...
0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 36525276
thx
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery 2 28
Need help with jQuery elements filtering 2 16
email validation 9 49
Building JSON/JQuery Results Display 11 26
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question