Skip to main content
deleted 4 characters in body
Source Link
cFreed
  • 2.8k
  • 15
  • 20

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-value="N"id="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class`class.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • Add the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.
    BTW here is an interesting demo from him, which illustrates how you can have fine control on the fadeToggle() execution.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-value="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class`.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • Add the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.
    BTW here is an interesting demo from him, which illustrates how you can have fine control on the fadeToggle() execution.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-id="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • Add the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.
    BTW here is an interesting demo from him, which illustrates how you can have fine control on the fadeToggle() execution.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}
added 172 characters in body
Source Link
cFreed
  • 2.8k
  • 15
  • 20

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-value="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class`.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • Add the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.
    BTW here is an interesting demo from him, which illustrates how you can have fine control on the fadeToggle() execution.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-value="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class`.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • Add the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-value="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class`.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • Add the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.
    BTW here is an interesting demo from him, which illustrates how you can have fine control on the fadeToggle() execution.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}
deleted 2 characters in body
Source Link
cFreed
  • 2.8k
  • 15
  • 20

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-value="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class`.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • AddedAdd the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-value="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class`.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • Added the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}

You can DRY your JS code, assuming you modify the HTML this way:

  • On each of the four selector <td>s, add a selector class and a data-value="N" attribute, where "N" refers to the #ElementN.
  • On each of the four selectable <td>s, add a selectable class`.

So your HTML part becomes:

<tr>
    <td class="selector" data-id="1"><img src="#" /><br />Inserer un produit</td>
    <td><img src="#" /><br /></td>
    <td class="selector" data-id="4"><img src="#" /><br />Inserer un nv crédit</td>
</tr>
<tr>
    <td class="selector" data-id="2">Afficher les produits</td>
    <td class="selector" data-id="3"><a href="#">Afficher les crédits courants</a></td>
</tr>
<!-- ElementN sample -->
<table id="Element1" class="selectable formInsertTab" ...

BTW, note that I affected data-value in the order showed by your demo (1, 4, 2, 3), while the code posted in your question is 1, 2, 3, 4.

Then in the JS part you can:

  • Take advantage of the above HTML changes to bind click event only once for all selectors.
  • Hide selectable elements without previously checking if they show.
    Warning, anyway your test was falsy: $('#ElementN').show always returns true, since it checks if the show() method exists, not if the element is showing!
  • Add the improvement proposed by @Roamer-1888: chain fadeToggle() before hiding other elements.

The resulting code is so much reduced:

$('.selector').click(function() {
  $element = $('#Element' + $(this).data('id')).fadeToggle('slow');
  $('.selectable').not($element).hide();
}
added 85 characters in body
Source Link
cFreed
  • 2.8k
  • 15
  • 20
Loading
Added precision
Source Link
cFreed
  • 2.8k
  • 15
  • 20
Loading
Source Link
cFreed
  • 2.8k
  • 15
  • 20
Loading