Search code examples
jqueryfindparentclosest

JQuery find() opposite


I want to find the next form-element, starting from input-object inside. Find() is a great function to find child objects. But what about the opposite Way to find in parent?

<form id="grabbMe">
    <div>
        <div>
            <div>
            <div><input type="text" value="test"></div>
            </div>
        </div>
    </div>
</form>

<script>
    $('input').findUp('form').attr('id')
</script>

Solution

  • You can use jQuery's closest() function to do this.

    $('input').closest('form').attr('id');