Solved

jquery getting overwritten by CSS?

Posted on 2011-09-12
13
322 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:knowlton
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
 
LVL 5

Author Comment

by: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:knowlton
ID: 36524994
>>>>Are your waiting the total loading of the page with the following for example :

$(document).ready(function() {

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


Yes
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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:knowlton
ID: 36525082
It seems to happen on the button click...

see screencast knowlton-499411.flv
0
 
LVL 5

Author Comment

by: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: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: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:knowlton
ID: 36525276
thx
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now