Solved

jquery getting overwritten by CSS?

Posted on 2011-09-12
13
321 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
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
They were all equal to 0.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
>>>>Are your waiting the total loading of the page with the following for example :

$(document).ready(function() {

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


Yes
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
It seems to happen on the button click...

see screencast knowlton-499411.flv
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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
Comment Utility
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
Comment Utility
with << code behind >>
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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
Comment Utility
thx
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Summernote required 3 56
Safari SVG Image Problem 1 19
jQuery Scroll on Mobile devices 1 20
gif overlay 3 32
DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

744 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

11 Experts available now in Live!

Get 1:1 Help Now