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
Solved

jquery getting overwritten by CSS?

Posted on 2011-09-12
13
326 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)
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…

840 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