Using Font Awesome 5 icon in JS

AI-generated summary

In a discussion titled ‘Using Font Awesome 5 icon in JS’ under the Dev category, zaino seeks help integrating a Font Awesome 5 icon (specifically the Twitter icon) into a JavaScript-generated <a> tag. Their current approach using <i class="fab fa-twitter"></i> isn’t working. dax suggests using an SVG-based approach instead, recommending the use of an <svg> element with the d-icon and svg-icon classes, and notes that the icon must first be added to the site settings under ‘svg icon’. The suggested syntax is: <svg class="fa d-icon d-icon-ICON-NAME svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use xlink:href="#ICON-NAME"></use></svg>. This provides a concrete alternative for rendering FA5 icons dynamically in JS.

I need some help here. I have read through all the threads regarding FA 5 and I am still not sure how to do this.

I want to use the FA icon as the link in an a tag within javascript. Here is how I would normally use the Twitter icon as the link, but it is not working:

if (userFields && userFields[userFieldId]) {
              const url = userFields[userFieldId];
              const link = "<a href='"+url+"' target='_blank'><i class="fab fa-twitter"></i></a>";
              return Ember.Object.create({ link, name: twitterUsernameField.get('name') });
            } else { ....

I am looking for a concrete example or the exact series of steps I need to take to get this to work.

Cheers

Something like this should works, but remember to add the new icon in the site setting svg icon

 const link = "<a href='"+url+"' target='_blank'><svg class="fa d-icon d-icon-ICON-NAME svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use xlink:href="#ICON-NAME"></use></svg></a>";