views:

18

answers:

2

Hello Guys,

I have many hrefs(with dynamic Ids) in my asp.net app that have the same CssClass=MyClass.

I want these button to be hidden with a condition.

I used the .ready

$(document).ready(function() {
  if(condition)
    $('.MyClass').css("display","none");
});

the problem is docuement.ready doesn't execut when there is a poctback.

Postback==>Button visible.normal as i've put the code in .ready.

Is there a way to persist the code:$('.MyClass').css("display","none");

I tried to apply .live() on button load,but it doesn't work.

Any ideas?

Thanks in advance.

A: 

Use the jQuery livequery plugin: http://brandonaaron.net/code/livequery/docs

live() only binds events. When you have installed the plugin, use:

$('.MyClass') 
.livequery(function() { 
    $(this).css("display","none");
});

This will hide items of class MyClass whenever they are found, even if they are created from a Ajax response. You can even use this code in place of the ready function you are currently using.

In this case, Nick Craver's solution is better, but only if you have to evaluate condition just on page load.

SimpleCoder
If using `.livequery()`, you should use `this` inside, instead of the same selector ...but this is overkill for the situation :)
Nick Craver
Ah, thank you; I just pasted that line and should have inspected it more carefully.
SimpleCoder
SOrry guys, but it doesn't work.
amourgh
what i have done is:added <script src="jquery.livequery.js" type="text/javascript"></script> to my aspx.after i added the code of SimpleCoder.
amourgh
+2  A: 

You can take a different approach, define the style in CSS, like this:

body.conditionClass .MyClass { display: none; }

Then apply that class to <body> on document.ready, like this:

$(function() {
  if(condition)
    $('body').addClass('conditionClass');
});

Now new elements with .MyClass, anywhere in the <body> will get the display: none styling.

Nick Craver
Nick,It doesn't work.have u tested an example.
amourgh
@amourgh - Yup, here's a demo: http://jsfiddle.net/nick_craver/ZZmBg/ change the condition to `false` and hit run to see the opposite.
Nick Craver
ya,your example works.I'm confused with this line:body.conditionClass .MyClass { display: none; } Where is the definition of MyClass .
amourgh
@amourgh - It's from your example code in the question...whatever class you're currently using would be what you put in for `MyClass` .
Nick Craver