views:

169

answers:

4

I still get a little confused when it comes to selectors and patterns... Basically, I'm trying to find the parent div of an input item and if a string is found anywhere in its ID, I want to set it to display none.

I've done this before by just doing something like:

if($('div[id*=string]')) { $(this).attr('display','none'); }

But, I'm not sure how to do that for a variable?

This is as far as I got, and then I get stuck...

$('input.rclass').each(function() {
    var myDiv = $(this).parent().parent();
    if($(myDiv...
});

The markup looks like this:

<div id="edit-gci" class="form-item">
   <label for="editgci[0][foa][value]" class="option">
     <input type="radio" class="rclass" value="" name="editgci[0][foa][value]" id="editgci-0-value-string-idnum"> N/A
   </label>
</div>
A: 

If you can assume it will always be

 $('input.rclass').each(function() {
      var myDiv = $(this).parent().parent();
      if(myDiv.attr("id").length >0){
           $(myDiv).hide(); 
      }
 });

Forgot to check if id attribute length > 0.

Chris
+1  A: 

The attr is used for setting element's attributes, so your line:

if($('div[id*=string]')) { $(this).attr('display','none'); }

Should be:

if($('div[id*=string]')) { $(this).css('display','none'); }

Use css method instead.

Sarfraz
A: 

In JavaScript, you can use a regular expression to look for a string within another string.

if(/string/.test($(myDiv).attr('id'))) { $(this).hide(); }
Victor Nicollet
I think you mean `$(myDiv).attr('id')` or `$(myDiv)[0].id`.
J-P
@J-P : yes, I did.
Victor Nicollet
A: 

You can use jQuery.closest()

$('input.rclass').cloest('.form-item').hide();
Dennis Cheung