Dynamic Form Attributes (eg. <label name="dynamic-content-here">)

For anybody interested, I achieved this by using some Jquery. In my use-case, I needed this to build a dynamic category filter interface for dynamic lists using the great tutorial from @sabanna (hence the ‘var filter’ names in the code).

  • Add a dynamic text block with the text you want as the attribute and give it a class.
  • Add the code below as custom code, and edit the class in the first line of the the code below with the class from step 1.
  • Change ‘for’ in the last line with whatever attribute you want to change (eg. ‘for’, ‘value’, ID etc).

Please note, I’m no JQuery expert, this worked for me but feel free to suggest a better way if anybody knows!

var filter = $(‘.course-filter-label’);
filter.each( function() {
var label = $(this);
var txt = label.text().toLowerCase();
label.attr(‘for’, txt);
});

2 Likes