Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  27] [ 2]  / answers: 1 / hits: 48266  / 13 Years ago, mon, december 19, 2011, 12:00:00

I have this javascript method:



<script type=text/javascript>
function MyFunction(sender, eventArgs) {
if (someCondition) {
//css
}
}
</script>


The css code I want to be executed is:



<style type=text/css>
.classInsideTheClassWhichEntersTheIf
{
background: url(Images/myImage.png) !important;
}
</style>


but only for those cells that enter the if condition above. If I write it outside it works but for every cell. Is something like this possible? If yes, how to do it?


More From » css

 Answers
47

There are several ways to do this.



Option 1.



<script type=text/javascript>
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.style.cssText = background: url(Images/myImage.png) !important;
}
}
</script>


Option 2.



 <script type=text/javascript>
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.className = someclass
}
}
</script>


where,



<style>
.someclass{
background: url(Images/myImage.png) !important;
}
</style>


Option 3



 <script type=text/javascript>
function MyFunction(sender, eventArgs) {
if (someCondition) {
someelement.setAttribute('style', 'background: url(Images/myImage.png) !important;');
}
}
</script>


Here is a pseudo code,



if(condition)
someelement.style.cssText = background: url(Images/myImage.png) !important;;

[#88488] Friday, December 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
monica

Total Points: 308
Total Questions: 102
Total Answers: 109

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
;