1

I'm trying to change an image using the replace command when toggling using jquery..basically showing a downward arrrow which becomes an upward arrow after the content is revealed. This following code works except the downward arrow doesn't get replaced.

<img src="/images/arrow_down.png" id="#1" class="nav-toggle">
<div id="1" style="display:none">some content</div>

and here is the jquery code

jQuery('document').ready(function($) {
    jQuery('.nav-toggle').click(function() {
        var collapse_content_selector = $(this).attr('id');
        var toggle_switch = $(this);
        $(collapse_content_selector).toggle(function() {
            if ($(this).css('display') == 'none') {

            } else {
                toggle_switch.attr("src").replace("down", "up");
            }
        });
    });
});​
2
  • 2
    IDs shouldn't start with numbers. Commented Sep 10, 2012 at 16:29
  • why you used the # in id is there any reason using this ? Commented Sep 10, 2012 at 16:30

6 Answers 6

2

You're not assigning it back to the src, try:

toggle_switch.attr("src", toggle_switch.attr("src").replace("down", "up"));
Sign up to request clarification or add additional context in comments.

Comments

2

Less code is better.

HTML

<a href="#" id="#arrow-toggle">
    <img src="/images/arrow_down.png">
    <img src="/images/arrow_up.png" class="up" style="display: none;">
</a>
<div id="content" style="display:none">some content</div>

jQuery

(function($) { 

    $('#arrow-toggle').click(function(event) {
        event.preventDefault();
        $('img:visible', this).hide().siblings().show();
        $('#content').toggle($('img:visible').is('.up'));
    });

})(jQuery);

Comments

2

You should use something like;

jQuery('.nav-toggle').click(function() {
        var collapse_content_selector = $(this).attr('id');
        var toggle_switch = $(this);
        $(collapse_content_selector).toggle(function() {
            if ($(this).css('display') == 'none') {
                toggle_switch.attr("src", toggle_switch.attr("src").replace("image_2.jpg", "image_1.jpg"));
            } else {
                toggle_switch.attr("src", toggle_switch.attr("src").replace("image_1.jpg", "image_2.jpg"));
            }
        });
    });

Here is a live demo.

Comments

0

I think you should try jQuery.UI.toggleSwitch()

Comments

0

HTML

<img src="/images/arrow_down.png" id="#1" class="nav-toggle">
<div id="1" style="display:none">some content</div>

jQuery

$(function(){
  $('.nav-toggle').on('click', function(){
    var t = $(this);
    var imgSrc = t.attr('src');
    var divId = t.attr('id');
    $(divId).toggle('slow', function() {
      if (imgSrc === '/images/arrow_down.png') {
        t.attr({'src' : '/images/arrow_up.png'});
      } else {
        t.attr({'src' :'/images/arrow_down.png'});
      }
     });
  });
});

Try this one

Comments

0
$("div#id").slideToggle(function() {
            if ($(this).css('display') == 'none') {
                $("div#id").css({'background-image':'url(css/img/nav_plus.gif)'});
            } else {
                $("div#id").css({'background-image':'url(css/img/nav_minus.gif)'});
            }
        });

another easy solution may be can help someone ;)

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.