views:

39

answers:

2

I need to hide a div that has a certain string in it, using contains.. but jQuery is hiding all div's that are parents of the target div too !

 $("div:contains('user_notifications')").css("display","none") 

how do I tell jQuery to just match the div that actually contains that string?

Thanks


Edit:

here's the inner div that contains the string I want to match..

<div class="group" style="display: block;">

 <input type="hidden" value="0" name="user[notification_options[new_reply]]" style="display: block;">

 <input type="checkbox" value="1" name="user[notification_options[new_reply]]" id="user_notification_options[new_reply]" checked="checked" style="display: block;">

 Request help from system admin

 </div>
+3  A: 

Assuming the div doesn't have divs that are siblings to the text, you can do this:

$("div:contains('user_notifications'):not(:has(div))").hide();

This uses :not() to get the opposite of :has() to only select <div> elements that don't contain other <div> elements, eliminating the parents. Also I'm using .hide() here as a simpler display: none;, in case you want to show it later.

Nick Craver
Nick - Thanks for the correction on my answer (deleted). +1 :o)
patrick dw
I included the html above.. but I think this works.. thanks much!
Joseph
I couldn't get it to work.
Gert G
@Joseph - Let me know if it doesn't....I don't see the `user_notifications` string in the posted HTML, you may need a slightly different selector, something like this: `$("div:has(input[name*='user[notification_options']):not(:has(div))")`.
Nick Craver
thanks for the help all.. I'm sure I can nail it with these ideas..
Joseph
A: 

By looking at your HTML, this would be more suitable:

$("input[name*=notification_options]").parent("div").hide();
Gert G