Manipulating the DOM by nesting an icon within a button element in HTML

Currently working on my todo list project and I have a question about using innerHTML. I'm curious if I can include an icon inside of a button tag like this:

btn.innerHTML = '<i class="fas fa-times fa-lg"></i>';

So, would that result in

<button><i class="fas fa-times fa-lg"></i></button>
?

Can someone explain the difference between the code above and the one below?

btn.innerHTML += '<i class="fas fa-times fa-lg"></i>';

Answer №1

To start, simply swap out the current content within the button for a fresh new icon

For example, if your button is labeled "click", this text would seamlessly transition to be represented by the newly added graphic symbol

In another scenario, an additional icon can be inserted immediately after the existing text "click"

Answer №2

btn.innerHTML = '<i class="fas fa-times fa-lg"></i>';

This particular code line will set the innerHTML of the btn.

btn.innerHTML += '<i class="fas fa-times fa-lg"></i>';

On the other hand, this code line will add

<i class="fas fa-times fa-lg"></i>
to the existing content inside btn. However, if btn is initially empty, it will produce the same result as
btn.innerHTML = '<i class="fas fa-times fa-lg"></i>';

For a simple demo, refer to the following:

var btn1 = document.getElementById("btn1");
var btn2 = document.getElementById("btn2");
var btn3 = document.getElementById("btn3");

btn1.innerHTML = '<i class="fas fa-times fa-lg"></i>';
btn2.innerHTML += '<i class="fas fa-times fa-lg"></i>';
btn3.innerHTML += '<i class="fas fa-times fa-lg"></i>';
<body>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" rel="stylesheet"/>
  <button id="btn1" >Something Inside</button>
  <button id="btn2" >Something Inside</button>
  <button id="btn3" ></button>
<body>

Answer №3

It's important to grasp the distinction between the two assignment statements

var name = (equals) sets the value to whatever you want

var name = "blaqkippah"
console.log(name)
blaqkippah

var age = 23
console.log(age)
23

(since variable name has already been declared, there is no need to declare it again with the var keyword)

var name = "blaqkippah"
console.log(name)
blaqkippah

The use of += in conjunction with strings involves joining them together. It provides a more concise way of writing code

name = name + "blah"

name += "deemyBoy"
console.log(name)
blaqkippahdeemyBoy // no space inserted as I wrote += "deemyBoy" not += " deemyBoy" 
ie. when joining 2 strings using += (or any other method) you have to manually add a 
space

var b = 'banana'
b
"banana"
b += ' apple'
"banana apple"
b
"banana apple"
b = 'apple'
b
"apple"

Numbers behave differently with += acting as the plus/addition operator, adding to what's already stored in b (with numbers you can also do -= (minus), *= (multiply) and /= (divide)

b = 2
console.log(b)
2
b += 3
console.log(b)
5
b = 4
console.log(b)
4

An example using minus:

b-=3
console.log(b)
1

An example using divide:

b /= 2
console.log(b)
0.5

An example using multiply:

b *= 50
console.log(b)
25

Returning to your question, I've utilized a JavaScript object so that innerHTML becomes a property on the object, allowing you to easily track the changing values in the console.logs

var btn = {} // declare empty JS object
undefined

btn.innerHTML = '<i class="fas fa-times fa-lg"></i>'; // assign value
console.log(btn)
{innerHTML: "<i class="fas fa-times fa-lg"></i>"}
btn.innerHTML += '<i class="fas fa-times fa-lg"></i>';
console.log(btn);
{innerHTML: "<i class="fas fa-times fa-lg"></i><i class="fas fa-times fa-lg"></i>"}
btn.innerHTML = '<i class="fas fa-times fa-lg">go back to original</i>';
console.log(btn);
{innerHTML: "<i class="fas fa-times fa-lg">go back to original</i>"}

This should provide a more comprehensive explanation for you

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

working with JSON array information

I have a JSON array retrieved from a database that I need to manipulate. Currently, it consists of 8 separate elements and I would like to condense it down to just 2 elements while nesting the rest. The current structure of my JSON looks like this: { "i ...

What is the best way to store multiple values in local storage using useState in react?

Currently, I am exploring how to utilize multiple values in the useState hook and perform CRUD operations in local storage. Here is an example of my code: const [Data,setData]=[[{ name:'luis', pass:'1234', //....... }]] // ...... ...

Difficulty encountered while deploying a React application on Netlify

I followed the instructions on a Medium link to deploy my React application on Netlify: To set up the production mode, I utilized an express server for defining build scripts. After creating the build scripts on my local machine, I uploaded them to the Ne ...

What is the best way to conceal Bootstrap accordion content when first loading my single-page application?

I am currently developing a single page application for an ESP32 device. I have chosen to keep all the content in one document as there is not a lot of information to display. My approach started with using the collapse class from Bootstrap 4.1.2. While i ...

The function did not execute properly, resulting in the express route returning no value

Encountering some issues with Express routes that are behaving inconsistently despite having identical code execution. The goal is to have a client application make API calls like this: async search(){ const query = this.$refs.searchInput.value; ...

The baffling quirks of variables within a Jquery loop

Unfortunately, I'm struggling to come up with a more fitting title for my question, but I'll do my best to provide a clear explanation of my issue. Here is the code snippet I am working with: let pdfInvoice_sub_template = [ {text: '{ ...

What is the significance of the "#" character in the URL of a jQuery mobile

As I encounter a strange issue with my jQuery mobile page, I notice that when accessing page.php everything looks good, but once adding #someDetailsHere to the URL, it only displays a blank white page. What could be causing this and how can I resolve it? ...

tips for aligning text to the right of an image

For my college project, I am working on a website. However, I have encountered an issue with using a jsp script to display products in a specific way on the webpage. The problem is that I cannot control where the information about price, name, and descript ...

"Successful implementation of Ajax function in local environment but encountering issues when running online

I am facing an issue with my AJAX function. It works perfectly fine on my local server but does not return anything when I move it to my online server. Below is the code snippet: This is the part of the page where I call the showEspece() function: echo ...

In JavaScript, the mousedown event consistently receives the "e" parameter as the event object

I am facing an issue while trying to handle a middle mouse button click event using JQuery on a DataTable from the website https://datatables.net/. Below is the code I have implemented. var tbl = document.getElementById("entries"); $(tbl).on('mousedo ...

Utilize the identical function value within a Vue template

In my template, I am encountering a recurring situation where I need to retrieve a value from a function. This function takes parameters from the template (such as the index of a v-for loop) and returns a result that is then displayed on the template. The ...

Boost Google Pagespeed score by reducing 'Total Blocking Time' in NextJs

I've recently started using NextJS and I'm looking to improve my Google Pagespeed ranking. So far, I've made some good progress in optimizing different metrics. From the screenshot provided, it's clear that the only issue remaining i ...

Tips for maintaining the fixed positioning of the left and right flex elements (sidebars) as the middle element scrolls, with the scroll bar located on the right side

I am currently designing a webpage with three main elements: a left sidebar, a right sidebar, and a central feed that is 600px wide and scrolls. The scroll bar must be positioned on the right side of the screen, allowing users to scroll by simply using the ...

How can the data from a factory be utilized within the same controller but in a separate file in an AngularJS application?

When trying to route from home.html to profile.html, there seems to be an issue with the execution of the profile.js script. I have data in script.js that is written in a factory. When the profile button is clicked, the page routes to profile.html, but the ...

Amazon S3 Landing Page Featuring Contact Form

Although S3 is not a fileserver, it serves as an excellent tool for managing static websites. The majority of my projects are 99% static, making it ideal for this particular project. As an AWS Solutions Architect, I am struggling to find the most straightf ...

Place a Button or Link Right Below the Title on the Custom Post Type Editing Page

I am currently working on customizing the backend of a WordPress site for a client by adding multiple custom post types. These custom posts are only meant to be displayed on the front-end through a specific loop on one page, preventing users from accessing ...

The Kendo Date Picker is failing to update properly when the k-ng-model is modified

I am facing an issue with my code involving two date pickers and one dropdown list. I want the date pickers to change based on the selected item from the dropdown list. Here is the relevant portion of my code: $scope.toolbarOptions = { i ...

Utilizing Multiple MongoDB Connections in a Node.js Application

I am facing an interesting challenge in my Node.js project where I need to connect multiple MongoDB databases. Let me share with you my current setup and the issue that is causing me some trouble. Node Version: v6.12.1 Express.js Version: 4.16.2 Mongoos ...

How can I make multiple elements in the same column span across multiple rows with CSS Grid?

Here is a scenario where the HTML and CSS (specifically the .type-a section) utilize CSS Grid. (Click here to view the CodePen example) The goal is to ensure that all elements with the .col1 class are aligned correctly in the first column, while having t ...

Looking to effortlessly move and arrange items with ng2-drag-drop in Angular 2?

I have encountered a problem with the ng2-drag-drop library. I am trying to drag and drop items from one div to another, and then freely move the dropped item within the droppable area. One issue I'm facing is that when I drop my draggable item in th ...