tags:

views:

75

answers:

4

Hi I have a div container having some string,Some child div have a text like "Welcome", I want to make that container div disabled i.e not clickable, If that container child div have "Welcome" string. My Markup is like this.

<div>
<span>some text</span>
<span>some text</span>
    <div>
        <span>some text</span>
        <span>some text</span>
    </div>
    <div>
       Welcome
    </div>
    <div>
       Welcome
    </div>

</div>
A: 
$('div:contains("Welcome")')
  .attr({ disabled: 'disabled' });

Or if you want to do only divs that have exactly Welcome.

$('div:contains("Welcome")').filter(function() {
    return $(this).html() === 'Welcome';
}).attr({ disabled: 'disabled' });

It appears clicking a disabled="disabled" element still fires its click event.

So you can unbind('click') instead, and leave the disabled if you like for semantic reasons (and as a CSS hook, i.e. div[disabled]).

alex
i don't think you can disable a click event just like that. and also if there was another element e.g. "you're welcome" that would include that element aswell.
Val
@Val: The second code example covers that. He also says child has text **like** `Welcome`. That is ambiguous, so I provided two solutions,.
alex
A: 

I´m no sure I understood your question, I hope this is what youre looking for:

 $("a").click(function(event){
   event.preventDefault();
   $(this).hide("slow");
 });

Please comment if not

Trufa
+2  A: 

If you want to disable a div, give it the class name "disabled", e.g.:

<div class="disabled">Whatever</div>

Then you can hook into all disabled divs' click events to prevent it from handling.

$('div.disabled').live('click', function(e) {
    e.stopPropagation();  // you might not want this depending on your intentions
    e.preventDefault();
    return false;
});
Matt Huggins
A: 

I presume you are using this as welcome message once user is logged in ... if user is loggedin you would have a link there... if is logged in then the user would just read welcome...

you need to do this on the server side scripting instead of client side.

anyways if you still need a client side solution.

$('div').each(function (){
  if($this.text().trim()=='Welcome'){
     $(this).click(function (){return false;})
  }
});

It would be a good idea if you use a class name as this is far better than just checking all the divs... the trim will remove all the white spaces before and after. so hope this helps.

Val