4

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?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231
  • What do you mean "only for cells that enter the condition"? Is your JavaScript defined inline? Where is your style definition? I do hope not inside the document body? –  Dec 19 '11 at 12:43
  • Please provide more details on your code. There are no cells in your code, we dont know what is sender, etc. – KSDaemon Dec 19 '11 at 12:44

4 Answers4

14

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;";
Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49
  • Yeah, call the MyFunction for each of your filtered in element in the for loop. Ill update hang on. – Ashwin Krishnamurthy Dec 19 '11 at 13:03
  • It gets called for each element, it's an event, I can't call it for specific elements. – petko_stankoski Dec 19 '11 at 13:03
  • Ok, whenever your condition is satisfied, then make the css changes on those elements using cssText property by appending the new change or by using a class that has all the properties of the old element along with the new css property which you want to add. – Ashwin Krishnamurthy Dec 19 '11 at 13:05
  • I've updated it, You need to keep track of your elements. Check for the position based on the for loop iterator count and set that element with the change you want. I cant tell you much from whatever you have provided. You need to show us even the markup. – Ashwin Krishnamurthy Dec 19 '11 at 13:11
5
<script type="text/javascript"> 
   function MyFunction(sender, eventArgs) { 
       if (someCondition) { 
          someelement.style.backgroundImage = "url(Images/myImage.png)"; 
       } 
    } 
</script> 

!important is unnecessary here, because inline styles override non-inline styles.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 4
    +1 for simplicity (even though you missed the opportunity to say "!important is unimportant here"). – nnnnnn Dec 19 '11 at 13:47
0

You can modify the style property of elements that you need. But more flexible method is to define a css-class with that css-code, and just add this class to filtered elements.

KSDaemon
  • 366
  • 2
  • 9
-1

Imagine that you have a cell with id="myCell" and your condition is true for what you want. Now you can do it using something like this:

$(document).ready(functio() {
   function SET(id) {
      $('#'+id).css('background-image','yourNewImage.png');
   }
   if (condition == true) {
      SET('myCell');
   }
});

Using .css, you can assign any value to each CSS property for each element. Here I changed the background-image.

Mohammad Saberi
  • 12,864
  • 27
  • 75
  • 127