Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written this script to find control and then add css class to it
var abc = $(this).parent().parent().parent().attr('id');

It works fine. When I test variable in alert like
alert(abc);

then it display correct id of the control. Now I want to add css class to this control. I tried this code but not working.
$('#abc').addClass('abc')
Posted
Comments
bbirajdar 15-Oct-12 9:55am    
What does it mean by not working?
Sergey Alexandrovich Kryukov 15-Oct-12 15:13pm    
You already added the class correctly. You should also have this class in CSS.
--SA

Make sure that the 'abc' that you add in the .addClass method is a valid css class that is available to the page. If it is, then it will work. If you don't see all the overrides that the class tries to apply, then you may have other css issues, but as long as your $('#abc') is valid then the .addClass('[validcssclassname]') will work.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Oct-12 15:14pm    
Correct, a 5. In other words, .addClass is correct.
I suspect OP has a big misconception, as if var abc had anything to do with CSS, but it doesn't.
--SA
TheCoolCoder 16-Oct-12 2:31am    
his problem was indeed what you both thought $('#abc') he was using wasnt valid..
This might seem silly but I am afraid you seem to be missing out on $('#'+abc) part.
$('#'+abc).addClass('abc')

Ignore if its not the issue :-).
 
Share this answer
 
v3
Comments
Dinesh Ambaliya 16-Oct-12 1:40am    
Thanks! But still I have problem. When I add css class it not work
$('#'+abc).addClass('abc'); css class name is correct I have created it.
but when I add css attribute it works
<pre>$('#' + abc).css('background', '#4A699C');</pre>
TheCoolCoder 16-Oct-12 2:11am    
I have seen some cases involving background images where the browser wouldnt load the images specified in css class initially unless some element in the page already uses the class.So when we change css class of element via script the browser wont be able to load that image all of a sudden. so try adding a dummy element of same type(as $(this).parent().parent().parent()) with css class 'abc' to page and keep it hidden. Suppose if the element to which you are adding class 'abc' is a div try keeping a dummy div <div id='divDummy' class='abc' style='display:none;'> </div> in your page, it could help. Let me know if this works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900