1

I am trying to check the image and replace the image for my expand and collapse <div> but i cant able to find the solution.

html code:

<h4 id="expanderHead">Virtual Room <span id="expanderSign">+</span></h4>
<div id="expanderContent" style="display:none">
    content ... content...
</div>

jquery:

$(document).ready(function(){
   $("#expanderHead").click(function(){
      $("#expanderContent").slideToggle();
      if ($("#expanderSign").text() == "+"){
          $("#expanderSign").html("-")
      }
      else {
          $("#expanderSign").text("+")
      }
    });
});

Here, instead of + and - i have to place <img src="" alt=""/> .

Thank you.

5
  • So what is the error you are getting? or actually what is the issue? Commented Aug 3, 2012 at 6:20
  • Just to mention but it is probably best to have the image already on the page, then just show it when you want to. This will stop the "flash" whilst the image loads when clicking the + Commented Aug 3, 2012 at 6:22
  • Also here you are checking like $("#expanderSign").text()=="+", so when the image came you need to check the whole image tag, i think it is not a good method.. Commented Aug 3, 2012 at 6:25
  • @mahesh : sorry my question is how to do? Actually i tried with attr() and innerHTML but its not working. Commented Aug 3, 2012 at 6:26
  • 1
    you can use $("#expanderSign").html().. Commented Aug 3, 2012 at 6:27

2 Answers 2

7

HTML

<h4 id="expanderHead">Virtual Room <span id="expanderSign"><img src="plus-sign.png" /></span></h4>
<div id="expanderContent" style="display:none">
    content ... content...
</div>​

EDITED (Added better jQuery Version)

jQuery New Version

$(document).ready(function(){

   $("#expanderHead").click(function(){

       $("#expanderContent").slideToggle();

       var plusImg = "http://cdn2.iconfinder.com/data/icons/diagona/icon/16/129.png";
       var minusImg = "http://cdn2.iconfinder.com/data/icons/diagona/icon/16/130.png";        
       $this = $("#expanderSign img");            

       if( $this.attr('src') == plusImg ) { $this.attr('src', minusImg);} 
       else { $this.attr('src', plusImg); }

   });
});

SEE LIVE DEMO

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

1 Comment

@Ran: Updated my answer with a better jQuery implementation. Discard the old suggestion. Thanks
0
$(document).ready(function(){

       $("#expanderHead").click(function(){
      $("#expanderContent").slideToggle();
      if ($("#imgId").attr('src') == "d2.png"){
          $("#imgId").attr("src","d1.png")
      }
      else {
          $("#imgId").attr("src","d2.png")
      }
    });
});

    <h4 id="expanderHead">Virtual Room <img id='imgId' src='d2.png' /></h4>
<div id="expanderContent" style="display:none">
    content ... content...
</div>

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.