Note that there are some explanatory texts on larger screens.

plurals
  1. POCleanup my javascript using a function
    primarykey
    data
    text
    <p>I have javascript code that pretty much does the same thing many times. Is there a way to make a function to clean this up a little bit?</p> <p>The two things I'd like to make into a function are:</p> <p><strong>bright()</strong></p> <pre><code>$(VARIABLE).find('.info').fadeTo('fast', 1); $(VARIABLE).find('img').fadeTo('fast', 1); </code></pre> <p><strong>light()</strong></p> <pre><code>$(VARIABLE).find('.info').fadeTo('fast', 0); $(VARIABLE).find('img').fadeTo('fast', 0.8); </code></pre> <p>.</p> <p>I want to be able to pass a variable to the function so I can change that specific part. I am new to javascript so I'll only be asking this sort of question once. Thanks in advance.</p> <pre><code>$(document).ready(function(){ var subject; $('.subject').mouseenter(function(){ if ( $(this).hasClass(subject) ) { return 0; } else { $(this).find('.info').fadeTo('fast', 1); $(this).find('img').fadeTo('fast', 1); } }); $('.subject').mouseleave(function(){ if ( $(this).hasClass(subject) ) { return 0; } else { $(this).find('.info').fadeTo('fast', 0); $(this).find('img').fadeTo('fast', 0.8); } }); $('.english-link').click(function(){ subject = 'english'; $('.english').find('.info').fadeTo('fast', 1); $('.english').find('img').fadeTo('fast', 1); }); $('.math-link').click(function(){ subject = 'math'; $('.math').find('.info').fadeTo('fast', 1); $('.math').find('img').fadeTo('fast', 1); }); $('.electives-link').click(function(){ subject = 'electives'; $('.electives').find('.info').fadeTo('fast', 1); $('.electives').find('img').fadeTo('fast', 1); }); $('.history-link').click(function(){ subject = 'history'; $('.history').find('.info').fadeTo('fast', 1); $('.history').find('img').fadeTo('fast', 1); }); $('.science-link').click(function(){ subject = 'science'; $('.science').find('.info').fadeTo('fast', 1); $('.science').find('img').fadeTo('fast', 1); }); $('.languages-link').click(function(){ subject = 'languages'; $('.languages').find('.info').fadeTo('fast', 1); $('.languages').find('img').fadeTo('fast', 1); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload