3

I created a page that displays post with comments using ajax To view the comments user must click comment button correspond to the post For simple understanding, example:

In HTML Block

<button id="btn1">cmt1</button>
<button id="btn2">cmt2</button>

<div id="cmt1">comments</div>
<div id="cmt2">comments</div>

'

I tried of using for loop as

for(var i=0;i<count;i++){
    $("#cmt"+i).hide();
    $("#cmtbtn"+i).click(function(){
       $("#cmt"+i).toggle('slow');
    });
}

But It works well for the last cmtbtn only and whenever i click all other buttons last comment box only displays

I'm saying that is there is a way of for loop or any looping

Update: My Js file is here http://jsfiddle.net/9ss50nzc

Santhosh Kumar
  • 543
  • 8
  • 22

4 Answers4

2

Try to give a same and unique class to all "Comment Button" and also to "Comment Div". Like example bellow :

<button id="btn1" class="comtbtn"> cmt1 </button>
<button id="btn2" class="comtbtn"> cmt2 </button>
<div id="cmt1" class="comt-div">comments1</div>
<div id="cmt2" class="comt-div">comments2</div>

Then catch the click event on class "comtbtn" . Bellow is the js script to hide show of comment -


    $('.comtbtn').click(function() {
       var btnId = $(this).attr("id");
       var id = btnId.slice(-1);
       $( "#cmt"+id ).toggle( "slow" );
    });

Add css to hide all the comment section when initially loaded to the DOM.


    .comt-div{
      display:none;
    }

GiriB
  • 166
  • 5
0

Start with giving your buttons and divs classes

<button id="btn1" class="actionButtons">cmt1</button>
<button id="btn2" class="actionButtons">cmt2</button>
<div id="cmt1" class="actionDivs" style="display:none">comments1<br/>more comments<br/>and even more</div>
<div id="cmt2" class="actionDivs" style="display:none">comments2<br/>another comment<br/>and another one</div>

Then loop through elements with that class name

for(var  i = 0; i < $(".actionButtons").length; i++){
    $(".actionButtons")[i].onclick = function () {
        for(var j = 0; j < $(".actionDivs").length; j++){
            if ($(".actionDivs")[j].id == this.innerText)
                $("#" + this.innerText).show();
            else
                $("#" + $(".actionDivs")[j].id).hide();
        }
    };
}

Fiddle

I hope this takes you closer to your solution.

Dumisani
  • 2,988
  • 1
  • 29
  • 40
0

Is this the sort of thing you want? A means of getting the index of the button which is equivalent to the indexed position of the hidden comment on your document?

You just hide the previous comment and display the new comment.

var oldNode = '';
function showComment = function(index) {

    if (typeof oldNode != undefined) $(oldNode).hide('slow');
    $('.comment:eq('+$(index).index()+')').show();
    oldNode = $('comment:eq('+$(index).index()+')');
}



<button onclick="showComment(this)">cmnt 1</button>
<button onclick="showComment(this)" >cmnt 2</button>

<div class='comment' style='display: none'>comment a</div>
<div class='comment' style='display: none'>comment b</div>
John
  • 1,309
  • 2
  • 12
  • 24
0

I got the answer it is possible to toggle inside loop by using traversing. Thanks everyone finally I sort out the answer.

Santhosh Kumar
  • 543
  • 8
  • 22