Pink401k
Pink401k

Reputation: 37

Multiple functions in a JS file

I want to have multiple functions to my JS file. Just not sure how to do that, since I'm really inexperienced with JavaScript. Here's what I have.

$(function(){
  $('.incentives').hide();

  $('.incentives-click').on("click", function(){
    $('.incentives').fadeToggle();
  });

    $('.incentivesB').hide();

  $('.incentivesB-click').on("click", function(){
    $('.incentivesB').fadeToggle();
  });

})();

I'm trying to add a bit that will make an image change when I hover over it.

$(function onHover(){
    $(".button").attr('src', 'images/donate2.png');
)}

$(function offHover(){
    $(".button").attr('src', 'images/donate1.png');
)}

BONUS

Also trying to get the image (.incentives-click) to toggle when clicked as well. I have an idea about how to go about this, but will appreciate if anyone wanted to help with this as well.

Thanks.

Upvotes: 1

Views: 512

Answers (2)

SwiftMango
SwiftMango

Reputation: 15304

$(function(){
    $('.button').hover(function(){
        $(this).attr('src', 'images/donate2.png')
    }, function(){
        $(this). attr('src', 'images/donate1.png');
    });

    $('.button').on('click',function(){
        $(this).toggle(function(){
             #change_image1
        }, function(){
             #change_image2
        });
    });
});

Upvotes: 1

Echilon
Echilon

Reputation: 10264

I wrote a jQuery plugin which does something similar to what you need. You should be able to modify it to suit your needs with minimal effort.

You could also start with one of the jQuery plugin boilerplates such as:

Upvotes: 1

Related Questions