How can I apply innerHTML to each item in a forEach loop?

I need the innerHTML of each <a> tag to change, so that if it says <a>facebook</a>, it should be changed to

<a><i class='fab fa-facebook'></i></a>
.

<main class="non">
 <div class="author-desc">
  <a href="#">facebook</a>, <a href="#">twitter</a>, <a href="#">github</a>
 </div>
</main>

I have written JavaScript code using querySelectorAll to target all elements, but I am unsure how to proceed after the forEach loop.

<script>
  let sosmed = document.querySelector(".author-desc");
  let sosmeds = sosmed.querySelectorAll("a");
  sosmeds.forEach(function (element) {
    if (element.innerHTML === "facebook") {
      element.innerHTML = "<i class='fab fa-facebook'></i>";
    }
  });
</script>

I would appreciate a detailed explanation on what steps I should take next.

I seem to be stuck with this piece of code as the innerHTML of the <a> tags is not changing.

sosmeds.forEach(function (element) {
  if (element.innerHTML === "facebook") {
    element.innerHTML = "<i class='fab fa-facebook'></i>";
  }

Answer №1

Remove the unnecessary function and correct any syntax mistakes.

let socialMediaLinks = document.querySelectorAll(".author-desc a");
socialMediaLinks.forEach(function(link) {
  if (link.innerHTML === "facebook") {
    link.innerHTML = "<i class='fa fa-facebook'></i>";
  }
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<main class="non">
  <div class="author-desc">
    <a href="#">facebook</a>, <a href="#">twitter</a>, <a href="#">github</a>
  </div>
</main>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Accessing specific elements of a multidimensional array using matrices

I am looking to extract a subset from a 3-dimensional array using three matrices, with each matrix representing one dimension. Here is an example: set.seed(1) A = array(sample(1:10,24, replace=TRUE), dim=c(3,4,2)) ind_dimension1 = matrix(c(1, ...

Using arrow functions in React to streamline unit testing with Jest

Every time I encounter an error that checkIfBlank is undefined, I recognize that I need to export the function. However, I am unsure how to do so for an Arrow Function. This is the code snippet in register.js import React from "react"; import ax ...

Does jQuery UI Layout have compatibility issues with jQuery version 3.3.1?

jQuery UI Layout is compatible with older versions of jQuery, but not with newer versions... Here is a working example: <html> <head> <script src="http://layout.jquery-dev.net/lib/js/jquery-1.4.2.js"></script> <script ...

Utilizing npm packages from third-party sources within a custom extension for personal use (not intended for distribution)

Exploring the idea of developing a basic Firefox extension that involves external modules such as Firebase and Cheerio, but there doesn't seem to be much information available on this topic. I noticed there are legacy options like jpm, but they'r ...

Throw an error if the entry is not found in the Firebase database

I have an array containing various objects. Users should be able to access all objects using moduls/, and a specific one with moduls/$id. However, if the requested modul does not exist, the database should return an error to inform the client that there is ...

What is the best approach to add additional functionality to an already existing object method?

Let's say we have an obj, var obj = { l:function(){ alert(1); } } In what way can additional functionality be incorporated into obj.l without directly modifying the object? ...

The act of coming back with just one array

I'm having trouble grasping the concept of returning a single array from a function that calls another function multiple times. The issue I'm facing is that each time the scrapingfunction runs, the console.log in the code provided outputs an arra ...

Storing various information using the asynchronous capabilities of Node.js and MongoDB

I have an array of IDs: var ids = ['53asd3','53asd2','53asd5']; Each ID corresponds to a document in MongoDB. I would like to create an object by retrieving data from each of them and saving it into another document. For exa ...

Steps for making a dropdown input field:1. Start by defining a

Is there a way to design a dropdown input using CSS or any other method? Take a look at this image for reference: Any suggestions on how to achieve it? ...

How can I use React Native to align two text tags differently within the same row?

In this particular scenario, I attempted to showcase two distinct elements within a list item. However, I encountered a challenge in figuring out how to display the "item.date" on the left side of the line, and the string "Click to show.&qu ...

Is there a way to identify a location in close proximity to another location?

With a position at (9,-3), I am looking to display all positions surrounding it within a square red boundary. However, I am struggling to find the algorithm to accomplish this task. Any help or alternative solutions would be greatly appreciated. Thank you ...

Is it recommended to utilize local storage as a means to retain and restore redux state and JWT tokens when browsers are restarted?

Are there specific guidelines on when to utilize local storage for storing state information if Redux is being used? For instance, in the case of an online form, Q1. Is it advisable to persist its state (current filled values) to localstorage, for exampl ...

Fade in and out effect for popups on Leaflet markers

As I delve into developing a map web app using Angular, one challenge I face is incorporating fading popups for markers. I envision these popups fading in and out on their own as per a timer, but I lack the know-how to achieve this: My current code for cr ...

What is the best way to retrieve data attributes from multiple div elements that share the same class name?

I have multiple div elements with the same class name. I want to extract the data attribute from these div's. I have tried some code and managed to get the result. However, I am unsure if this is the best approach for retrieving data("cartid"). Any as ...

Creating a responsive HTML5 webpage that adjusts seamlessly to all screen sizes without the need for scrolling

I have developed an HTML5 application for mobile devices. Currently, I have managed to address width-height issues by using a simple scroll feature when necessary. However, I have encountered a challenge with a particular page that contains a lot of conten ...

Is there a way to evenly space out the buttons in the Nav element with more room in between each one?

I need the buttons to be evenly spaced with more room between them. And, of course, they need to be responsive in the end. I attempted to use Bootstrap's "d-flex" class to arrange the buttons, but it didn't work as expected. If you review my Boo ...

Angular app experiencing issues with material table row selection and hovering functionality not functioning as expected

I am working on a straightforward project that involves testing material row selection and hovering. The project is available on Stack Blitz at basic-table-test. I have a few questions regarding this: Why does the selection disappear when hovering the mou ...

Encountering an error while configuring webpack with ReactJS: Unexpected token found while

I'm attempting to update the state of all elements within an array in ReactJS, as illustrated below. As a newbie to this application development, it's challenging for me to identify the mistake in my code. closeState(){ this.state.itemList.f ...

Enhance the output of an array by incorporating additional text into the values

I'm diving into the world of Apps Script and I have a task at hand - adding some text to each value in an array. However, the code I currently have simply moves those values to another column: function getData() { var sheet1 = SpreadsheetApp.getAct ...

Error when parsing JSON due to the presence of backslashes within the serialized object

When trying to call a server side function and parse the response in client side using JavaScript and Ajax, I encountered a parse error. It seems that the issue lies with the backslash that the JavaScriptSerializer adds to serialize the object. The respons ...