skip to Main Content
<div class="container">
    <div class="form">
        <div class="row">
        </div>
        <div class="row">
        </div>
    </div>
    <div class="form">
        <div class="row">
        </div>
        <div class="row">
        </div>
    </div>
</div>

New to jquery and was wondering how do you select a specific child of a specific child. Say I want to hide the second row from the second form. I’ve tried

$('.container:nth-child(2)').eq(1).css("display", "none")

and

$('.container').children("form").eq(1).css("display", "none")

but can’t seem to get to the specific child of a specific child.

2

Answers


  1. First you have to find the first element : $(‘.container .form’).eq(1) then get the second element : .find(‘.row’).eq(1)

    $('.container .form').eq(1).find('.row').eq(1).css("display", "none");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="container">
        <div class="form">
            <div class="row">1
            </div>
            <div class="row">2
            </div>
        </div>
        <div class="form">
            <div class="row">1
            </div>
            <div class="row">2
            </div>
        </div>
    </div>
    Login or Signup to reply.
  2. Use form as class.

    jQuery(".container .form:eq(1) .row:eq(1)").css('display', 'none')
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="container">
        <div class="form">
            <div class="row">
            I am visible from form 1 row 1
            </div>
            <div class="row">
            I am visible from form 1 row 2
            </div>
        </div>
        <div class="form">
            <div class="row">
            I am visible from form 2 row 1
            </div>
            <div class="row">
            I need to be hidden
            </div>
        </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search