1

Whenever I click the comment link all of the divs are opening but i need that particular only to be opened.

HTML:

<div>
    <a href="#" class="ck">comment</a>
    <div class="cmt">
        <input type="text" class="txt"/>
        <input type="button" value="submit"/>
    </div></div>
<div>
    <a href="#" class="ck">comment</a>
    <div class="cmt">
        <input type="text" class="txt">
            <input type="button" value="submit">
            </div></div>
        <div>
            <a href="#" class="ck">comment</a>
            <div class="cmt">
                <input type="text" class="txt"/>
                <input type="button" value="submit"/>
            </div></div>
        <div>
            <a href="#" class="ck">comment</a>
            <div class="cmt">
                <input type="text" class="txt"/>
                <input type="button" value="submit"/>
            </div></div>

jQuery:

$(document).ready(function(){
    $(".cmt").hide();
    $(".ck").click(function(){
        $(".cmt").show();
    });
});

jsfiddle: http://jsfiddle.net/48gfje0f/

2 Answers 2

1

You can use jQuery nextAll() and then filter for the first matching element:

$(".cmt").hide();
$(".ck").click(function(){
    $(this).nextAll(".cmt:first").show();
});

Demo: http://jsfiddle.net/48gfje0f/3/

Also, instead of using show() you could use toggle(), which will show and hide the relevant div when you click on the link.

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

Comments

0

You need to use this to "find" the required elements. In this case $(this).next(), because .cmt is the adjacent sibling of .ck. Just like:

$(".ck").click(function(){
    $(this).next().show();
});

DEMO

2 Comments

what to do if their is another element in between .ck and .cmt?
As @Andrew say in the other answer, $(this).nextAll(".cmt:first").show(); should work.

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.