Solved

jquery getting overwritten by CSS?

Posted on 2011-09-12
13
330 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

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…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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…

729 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