-1

I want to do a foreach with each in jquery.

My function jquery have a call for another function showTauxPlafonds so i want for every a i have a call for this function to return a result with ajax.

For every <a> I want when I click I have a call for my function jquery :

$("a").each(function() {
  $(this).on("click", function() {
    showTauxPlafonds(urlTaux, $(this).attr('tauxPlafonds'));
  })
});


var showTauxPlafonds = function(url, tauxPlafonds) {
  if (tauxPlafonds != null)
    getAjaxedHtmlView(url, {
      tauxPlafonds: tauxPlafonds
    }, null, 'app_taux_plafonds_values');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="mdc-list-item" tauxPlafonds="PSS" data-toggle="tooltip" data-placement="top" title="Acceuil" style="cursor:pointer;width:350px">
Plafond sécurité sociale
</a>
<a class="mdc-list-item" data-toggle="tooltip" data-placement="top" title="Taux et plafonds" style="cursor:pointer;width:350px">
Smic
</a>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
mecab1995
  • 163
  • 1
  • 2
  • 11
  • 4
    You can bind directly to the anchor, no need for the each - `$('a').on("click", function() {` although your code should work - what is your exact problem? Is it that the links are loading a new page so it looks as if your code is not hit? Or that `$(this)` is not what it seems in `showTauxPlafonds(urlTaux, $(this).attr('tauxPlafonds'));` – Pete Oct 02 '18 at 09:36
  • 1
    I made you a proper snippet. Please add at least a dummy `getAjaxedHtmlView` – mplungjan Oct 02 '18 at 09:37
  • this line - `showTauxPlafonds(urlTaux, $(this).attr('tauxPlafonds'));` - where is `urlTaux` defined? – treyBake Oct 02 '18 at 09:38
  • @Pete How i do that ? – mecab1995 Oct 02 '18 at 09:38
  • 1
    How can you do what? You haven't said what your problem is? – Pete Oct 02 '18 at 09:39
  • Have a read of [mcve]. You should be able to generate some example code *without* the ajax call (ie with a dummy function) to demonstrate exactly *where* your code problem is (hint: given the code provided, it's probably not the `.each`) – freedomn-m Oct 02 '18 at 09:39

2 Answers2

0

I think you mean this, assuming url is the possible href of the link

There is no need for the showTauxPlafonds function since it is just testing the attribute which WILL be set unless you have tauxPlafonds="" in your code. Then you just test it in the selector:

var urlTaux = "someurl";
$("a[tauxPlafonds!='']").on("click", function() {
  getAjaxedHtmlView(urlTaux, { 
    tauxPlafonds: $(this).attr("tauxPlafonds")
  }, null, 'app_taux_plafonds_values')
});

I do suggest however you do data-tauxPlafonds="PSS" to not create new type of attributes

See jQuery: Select data attributes that aren't empty? for more example of testing an attribute

mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

This method also solved my problem :

$("a[tauxPlafonds]").on("click", function () {
showTauxPlafonds(urlTaux, $(this).attr('tauxPlafonds'));
});
mecab1995
  • 163
  • 1
  • 2
  • 11
  • Yes, but that is inefficient since all showTauxPlafonds does is test if the attr is empty which it is not since $("a[tauxPlafonds]") gets all As with the attribute – mplungjan Oct 02 '18 at 11:23