0

I have a following Javascript function for expand collapse of the content:

function showAns(inp){

        var hide="#hideAns"+inp;
        var show="#showAns"+inp; 
        var ansdiv ="#ans"+inp;
      $(hide).click(function(){
            $(ansdiv).hide(500);
            $(hide).hide();
            $(show).show();
        });
          $(show).click(function(){
            $(ansdiv).show(500);
            $(hide).show();
            $(show).hide();
        });
    }

This function is called from following piece of generated HTML code:

 <div class="qbox">
        <span class="qclass">Q. No.1) This is test question 112
            <img id ="showAns1" class="icons" src="img/expand.png" onclick="javascript:showAns(1)" alt="show" width="20" height="20" style="float:right; cursor:pointer;">
            <img id="hideAns1" class="icons" src="img/collapse.png" onclick="javascript:showAns(1)" alt="hide" width="20" height="20" style="float:right; display:none; cursor:pointer;">
        </span>
        <img alt="image" class="img" src="img/sample.jpg">
        <hr />
        <span id="ans1">This is test answer 112</span>
    </div>
    <br>

    <div class="qbox">
        <span class="qclass">Q. No.2) This is test question 110
            <img id ="showAns2" class="icons" src="img/expand.png" onclick="javascript:showAns(2)" alt="show" width="20" height="20" style="float:right; cursor:pointer;">
            <img id="hideAns2" class="icons" src="img/collapse.png" onclick="javascript:showAns(2)" alt="hide" width="20" height="20" style="float:right; display:none; cursor:pointer;">
        </span>
        <hr />
        <span id="ans2">This is test answer 110</span>
    </div>
    <br>

problem here is that I have to click thrice the image first time then only expand collapse works fine. Once I click the image twice after that expand collapse works fine. I don't know what I am doing wrong, any help will be highly appreciated. Thank you.

5
  • Can you post the rendered html, there are many alternative easy ways to accomplish your needs. Commented Jul 11, 2014 at 10:50
  • mind making a jsfiddle? Commented Jul 11, 2014 at 10:50
  • 3
    As you are using jQuery. Why on earth you are using inline event handlers? And In function showAns() You are only binding events not executing them Commented Jul 11, 2014 at 10:50
  • And of course I am new to jQuery so my code may look stupid. Commented Jul 11, 2014 at 10:56
  • The code you posted binds events to the elements, so that when that element is clicked it will expand/collapse. You want to run that code on $(document).ready() not as an onClick event. It works the second time because after you click it the first time, it has bound the events. Commented Jul 11, 2014 at 10:59

3 Answers 3

1

You can simply do like this

$("[id^=ans]").hide();
$(".icons").click(function(){
    $(this).closest("span").find("img").toggle();
    $(this).closest(".qbox").find("[id^=ans]").slideToggle();
});

Fiddle

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

Comments

1

I would recommend you to use class selector and bind events using jQuery. Here is an example.

HTML

<div class="qbox">
    <span class="qclass">QUESTION
        <img class="show icons" src="img/expand.png" alt="show" >
        <img class="hide icons" src="img/collapse.png" alt="hide" >
    </span>
    <hr />
    <span class="ans">Answer</span>
</div>

Script

$('.qbox .show').click(function(){
    var closestQbox = $(this).closest('.qbox');
    closestQbox.find('.ans').show(500);
    closestQbox.find('.hide').show();
    $(this).hide();
});
$('.qbox .hide').click(function(){
    var closestQbox = $(this).closest('.qbox');
    closestQbox.find('.ans').hide(500);
    closestQbox.find('.show').show();
    $(this).hide();
});

A simplified version of script

$(".ans, .hide").hide();
$('.qbox .icons').click(function () {
    var closestQbox = $(this).closest('.qbox');
    closestQbox.find('.ans, .hide, .show').toggle();
});

DEMO

Comments

0

Try this code :

$(function(){
  $('.icons').click(function(){
        $(this).hide();
        $(this).siblings().show();
        $(this).closest('.qbox').find('[id^=ans]').toggle(500);
  });

});

Comments

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.