Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery show next parent div

I have the following HTML markup:

<div id="step1" class="step">
                    <h1>Enter code</h1>
                    <p><input type="text" value="<%= @groupCode %>" name="group_code" class="span3" />
                        <a class="btn btn-primary btn-medium">
                            Next &raquo;
                        </a>
                    </p>
                </div>
                <div id="step2" class="step" style="display: none">
                    <p>Hello World</p>
                </div>

Upon clicking the link (.btn) I am needing to show the next parent div (#step2). I am designing this as a registration process, so there will be multiple parent divs (all named with step{:id})

Any help is much appreciated!

like image 842
dennismonsewicz Avatar asked Apr 16 '12 17:04

dennismonsewicz


People also ask

How to find parent div in jQuery?

The parent() is an inbuilt method in jQuery which is used to find the parent element related to the selected element. This parent() method in jQuery traverse a single level up the selected element and return that element. Here selector is the selected elements whose parent need to find.

Where can I find grandparents in jQuery?

jQuery parentsUntil() Method The parentsUntil() method returns all ancestor elements between the selector and stop. An ancestor is a parent, grandparent, great-grandparent, and so on.

What does $( Div parent will select?

The :parent Selector page on jQuery says: Select all elements that have at least one child node (either an element or text). So $('div') would select all divs and $('div:parent') would select only those with children.


2 Answers

You should be able to use something like:

//This should access the parent (step1), then the next element (step2)
$(this).parent().next()
like image 132
Rion Williams Avatar answered Sep 22 '22 12:09

Rion Williams


jQuery('.btn').click(function(){
    jQuery(this).closest('.step').next().show();
});
like image 39
mprabhat Avatar answered Sep 25 '22 12:09

mprabhat