Solved

how do you swap an image with Jquery

Posted on 2014-01-16
6
252 Views
Last Modified: 2014-01-16
Hi,

I have a function that works great, but instead of using $elem.text() I need to swap some images.

Here is my function:

<script type="text/javascript">

$(document).ready(function () {
    $("#toggle_all_dates").on('click', function () {
        var $this = $(this);
        var setToHideAll = false;
        
        if($this.hasClass('showAll')){
            $('#dates_list li div.moredates')
                .not(':visible')
                .prev()
                .each(function(idx,item){ toggleDate(item); });
                setToHideAll = true;
        }else{
            $('#dates_list li div.moredates:visible')
                .prev()
                .each(function(idx,item){ toggleDate(item); });
        }
        
        updateShowAll(setToHideAll);
    });    
    
    $("#dates_list li a").on('click', function () {
        toggleDate(this);
    });
    
    function updateShowAll(setToHideAll){
        if (setToHideAll) {
            $('#toggle_all_dates').removeClass('showAll');
            $('#toggle_all_dates').html('Hide all Dates and Locations');
        } else {
            $('#toggle_all_dates').addClass('showAll');
            $('#toggle_all_dates').html('Show all  Dates and Locations');
             
        }
    }

    function toggleDate(elem){
        var $elem = $(elem);
        var setToHideAll = false;
   
        if(! $elem.closest('tr').find('div.moredates').is(':visible') && $('#dates_list li div.moredates:visible').length === $('#dates_list li div.moredates').length - 2){
            setToHideAll = true;
        }
        $elem.closest('tr').find('div.moredates').slideToggle();
            $elem.html($elem.text() == '[-]' ? '[+]' : '[-]');

        updateShowAll(setToHideAll);

    }
})

</script>

Open in new window


How do you swap out the "$elem.html($elem.text() == '[-]' ? '[+]' : '[-]');" and use images instead?

Thanks!
0
Comment
Question by:smfmetro10
  • 3
  • 3
6 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Assuming you have the image to start with in the html

$elem.attr("src")==($elem.attr("src") == "minus.jpg" ? plusminus=="plus.jpg" : plusminus=="minus.jpg");
0
 

Author Comment

by:smfmetro10
Comment Utility
Thanks for the reply. How do you give an image an id then reference it?

Here is the code that Im working with:
<script type="text/javascript">

$(document).ready(function () {
    $("#toggle_all_dates").on('click', function () {
        var $this = $(this);
        var setToHideAll = false;
        
        if($this.hasClass('showAll')){
            $('#dates_list li div.moredates')
                .not(':visible')
                .prev()
                .each(function(idx,item){ toggleDate(item); });
                setToHideAll = true;
        }else{
            $('#dates_list li div.moredates:visible')
                .prev()
                .each(function(idx,item){ toggleDate(item); });
        }
        
        updateShowAll(setToHideAll);
    });    
    
    $("#dates_list li a").on('click', function () {
        toggleDate(this);
    });
    
    function updateShowAll(setToHideAll){
        if (setToHideAll) {
            $('#toggle_all_dates').removeClass('showAll');
            $('#toggle_all_dates').html('Hide all Dates and Locations');
        } else {
            $('#toggle_all_dates').addClass('showAll');
            $('#toggle_all_dates').html('Show all  Dates and Locations');
             
        }
    }

    function toggleDate(elem){
        var $elem = $(elem);
        var setToHideAll = false;
   
        if(! $elem.closest('tr').find('div.moredates').is(':visible') && $('#dates_list li div.moredates:visible').length === $('#dates_list li div.moredates').length - 2){
            setToHideAll = true;
        }
        $elem.closest('tr').find('div.moredates').slideToggle();
            $elem.html($elem.text() == '[-]' ? '[+]' : '[-]');

        updateShowAll(setToHideAll);

    }
})

</script>

            <tr>
<td>
                                <li>
                                 <a>[+]</a>
                              <div class="moredates" data-collapsed="true">
                                       More info.....
                               </div>
                                         </li></td></tr>

Open in new window


Thanks for the help!!
CME-more.png
CME-less.png
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Ignore that bit, I amended the code.

You just need to change to
$elem.prop("src")==($elem.prop("src") == "minus.jpg" ? "plus.jpg" : "minus.jpg");

And replace
<a>[+]</a>

with your plus image i.e
<a><img src="plus.jpg"></a>

Screwed that up again - amended code
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:smfmetro10
Comment Utility
So, this is what I have so far.  It is not opening all the div's at once now, but rather opening them one at a time. Also the images are not swapping.

<script type="text/javascript">

$(document).ready(function () {
    $("#toggle_all_dates").on('click', function () {
        var $this = $(this);
        var setToHideAll = false;
        
        if($this.hasClass('showAll')){
            $('#dates_list li div.moredates')
                .not(':visible')
                .prev()
                .each(function(idx,item){ toggleDate(item); });
                setToHideAll = true;
        }else{
            $('#dates_list li div.moredates:visible')
                .prev()
                .each(function(idx,item){ toggleDate(item); });
        }
        
        updateShowAll(setToHideAll);
    });    
    
    $("#dates_list li a").on('click', function () {
        toggleDate(this);
    });
    
    function updateShowAll(setToHideAll){
        if (setToHideAll) {
            $('#toggle_all_dates').removeClass('showAll');
            $('#toggle_all_dates').html('Hide all Dates and Locations');
        } else {
            $('#toggle_all_dates').addClass('showAll');
            $('#toggle_all_dates').html('Show all  Dates and Locations');
             
        }
    }

    function toggleDate(elem){
        var $elem = $(elem);
        var setToHideAll = false;
   
        if(! $elem.closest('tr').find('div.moredates').is(':visible') && $('#dates_list li div.moredates:visible').length === $('#dates_list li div.moredates').length - 2){
            setToHideAll = true;
        }
        $elem.closest('tr').find('div.moredates').slideToggle();
            
            $elem.attr("src")==($elem.attr("src") == "images/CME-less.png" ? plusminus=="images/CME-more.png" : plusminus=="images/CME-less.png"); 

        updateShowAll(setToHideAll);

    }
})

</script>

<tr>
<td>
<li>
             <a ><img src="images/CME-more.png"></a>
                              <div class="moredates" data-collapsed="true">
                                More info....
                                </div>

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
$("img",elem).attr("src",$("img",elem).attr("src") == "images/CME-less.png" ? "images/CME-more.png" : "images/CME-less.png");
0
 

Author Closing Comment

by:smfmetro10
Comment Utility
Outstanding! Thanks!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now