3

In a document, I have about 25 DIVs which contain forms with various names. The DIVs all have the same class name and look like this:

<div class="swoop" style="display:none">
  <form name="myFormXYZ" method="post">
    <input name="caption" type="hidden" value="19">
  </form>
</div>

I want to write jQuery code which will check all DIVs with the class of "swoop" and if any DIVs contain an input field named "caption" which has a value of x, then the display property of those DIVs should be set to "block". x will be an integer between 1 and 1000.

So far I have come up with this:

$('.swoop').each(function() {
    var capt = $( ? ? ? ).attr('value()');
    if (capt == x) {$(this).css.(display','block')}
});

Can the jQuery function even iterate through the DIVs whose display is set to none?

1
  • An element with display: none is not rendered, but it is still in the DOM. So yes, $('.swoop') will select all elements with class .swoop. Commented Jan 24, 2013 at 21:42

6 Answers 6

7

Yet another way:

$('.swoop').has('input[name=caption][value=' + x + ']').show();

This uses attribute selectors instead to find the input elements with a certain value. .has filters the elements by those that have descendants matching the passed selector and .show should be self-explanatory.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, sorry, that was my bad... my display made it look like there was a space btw the selectors.
Already six answers! Thank you, Felix, @Omar and @Musa! I will not have time until tomorrow to check these out.
Thank all six of you for answering my post. I wrote thank you notes as comments to all six but they have all disappeared. ?
This works perfectly, chosen as the accepted answer for being the shortest solution, also teaching me something about filters that was not in my book. Thank you!
1

you need:

$('.swoop').each(function() {
    var capt = $('input[name=caption]', this).val();
    if (capt == x) {$(this).css.('display','block')}
});

1 Comment

Funny thing, the code in your answer looks to me to be exactly the same as the answer of Matt just above, but my MSIE debugger gives a different error message for your solution: 'expected identifier'. I know, the MSIE debugger is usually not worth much... Sorry that I don't have time to find out if the problem is with my code.
1
$('.swoop input[name="caption"]').each(function() {
    var capt = $(this).val();
    if (capt == x) {
        $(this).closest('.swoop').css('display','block')
    }
});

2 Comments

input[caption] won't select any element. No element has an attribute caption.
Thanks for your suggestion! It was the only one of those here that worked for me, except for the first one above, which I accepted as the answer because it was the shortest.
1
var someInt = 7;    
$('.swoop').each(function() {
    var capt = $('input[name=caption]', this).val());
    if (capt == someInt) {$(this).css('display','block');}
});

3 Comments

Thank you, Matt. I will not have time until tomorrow to try out these solutions!
My MSIE debugger kept saying "expected ;" but I couldn't find where one was missing.
@user1781623, I found it. It was missing in the "if" block. Thanks for the heads up.
0

Yes, jQuery can access elements even if they are hidden.

Use the current item as context in the jQuery call to look for elements inside it. Use the val method to get the value of the input, and you would want to parse the value to turn the string into a number:

var capt = parseInt($('input[name=caption]', this).val(), 10);
if (capt == x) { $(this).css('display','block'); }

Comments

0
var v = 19;    
$('.swoop input[name=caption]').each(function () {
            var capt = $(this).attr('value');
            if (capt == v) { 
            $(this).parents("div").andSelf().css('display', 'block');
            $(this).attr({ type: '' }); }
        });

1 Comment

Trying this out, got the error message "Type property can't be changed". Sorry, no time to troubleshoot whether the problem is in my code or not, since the first answer above worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.