Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery - Grouping Repetitive Function into a Class Question
    primarykey
    data
    text
    <p>I am trying to figure out a way to create a class to group together a set of effects that performs the same effects but just different color. I am a procedural guy and OOP is something I am still learning and need some guide.</p> <p>I am trying to create a 'tag' lookalike shape using 'a' tag that changes the font and background color when mouseover, mouseout or when clicked. But I want to <strong>reuse</strong> the code for many other color variation so I hate to keep typing the code over and over again.</p> <p>How do I group the function like a Jquery plugin?</p> <p><code>**CSS**</code></p> <pre><code>a.cbSizeTag { display: inline-block; padding: 4px 4px 4px 4px; margin: 0 2px 6px 0; background: #F5F5F1; text-decoration: none; } a.cbShipmentTag { display: inline-block; padding: 4px 4px 4px 4px; margin: 0 2px 6px 0; background: #F5F5F1; text-decoration: none; } </code></pre> <p><code>**Jquery**</code></p> <pre><code>$('.cbSizeTag').mouseover(function() { $(this).css('background-color','#076EA0') $(this).css('color','#FFF') }); $('.cbSizeTag').mouseout(function() { if ($(this).hasClass('selected')){ $(this).css('background-color','#076EA0') $(this).css('color','#FFF') } else { $(this).css('background-color','#F5F5F1') $(this).css('color','#4B94BF') } }); $('.cbSizeTag').click(function() { $(this).toggleClass('selected'); if ($(this).hasClass('selected')){ $(this).css('background-color','#076EA0') $(this).css('color','#FFF') } else { $(this).css('background-color','#F5F5F1') $(this).css('color','#4B94BF') } }); $('.cbShipmentTag').mouseover(function() { $(this).css('background-color','#B80000') $(this).css('color','#FFF') }); $('.cbShipmentTag').mouseout(function() { if ($(this).hasClass('selected')){ $(this).css('background-color','#B80000') $(this).css('color','#FFF') } else { $(this).css('background-color','#F5F5F1') $(this).css('color','#4B94BF') } }); $('.cbShipmentTag').click(function() { $(this).toggleClass('selected'); if ($(this).hasClass('selected')){ $(this).css('background-color','#B80000') $(this).css('color','#FFF') } else { $(this).css('background-color','#F5F5F1') $(this).css('color','#4B94BF') } }); </code></pre> <p><code>**HTML**</code></p> <pre><code>&lt;a href="javascript:void(0)" class="cbSizeTag"&gt;S&lt;/a&gt; &lt;a href="javascript:void(0)" class="cbSizeTag"&gt;M&lt;/a&gt; &lt;a href="javascript:void(0)" class="cbSizeTag"&gt;L&lt;/a&gt; &lt;br&gt; &lt;a href="javascript:void(0)" class="cbShipmentTag"&gt;DHL&lt;/a&gt; &lt;a href="javascript:void(0)" class="cbShipmentTag"&gt;FEDEX&lt;/a&gt; &lt;a href="javascript:void(0)" class="cbShipmentTag"&gt;Free&lt;/a&gt; </code></pre> <p>So if I were to create another 10 functions with different colors, I would have create 10 sets of CSS, 10x3 sets of Jquery code and I think there must be a better way to write it. Can anyone help with a class-template example so I can learn?</p> <p>Thanks.</p>
    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.
    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