Issues with IE not recognizing the appendChild() Javascript method

Can someone please help me troubleshoot why this code snippet is not functioning properly in Internet Explorer but works fine in Firefox? I am fairly new to using the appendChild() method.

<html>
<head>
<script type='text/javascript'>

function createTable()
{
   let newTable = document.createElement('table');
   newTable.setAttribute('id', 'myNewTable');
   newTable.setAttribute('border', '1');

   let row1 = document.createElement('tr');
   let cell1 = document.createElement('td');
   cell1.setAttribute('colspan', '2');
   let centerCell1 = document.createElement('center');
   let boldText = document.createElement('b');
   boldText.appendChild(document.createTextNode('Title'));
   centerCell1.appendChild(boldText);
   cell1.appendChild(centerCell1);
   row1.appendChild(cell1);

   let row2 = document.createElement('tr');
   let cell2_1 = document.createElement('td');
   let centerCell2_1 = document.createElement('center');
   centerCell2_1.appendChild(document.createTextNode('21'));
   cell2_1.appendChild(centerCell2_1);
   let cell2_2 = document.createElement('td');
   let centerCell2_2 = document.createElement('center');
   centerCell2_2.appendChild(document.createTextNode('22'));
   cell2_2.appendChild(centerCell2_2);
   row2.appendChild(cell2_1);
   row2.appendChild(cell2_2);

   newTable.appendChild(row1);
   newTable.appendChild(row2);

   alert('Almost there!');
   try
   {
     document.getElementById('mainContainer').appendChild(newTable);
   }
   catch(error)
   {
     alert(error.message);
   }
   return;
}

</script>
</HEAD>
<BODY>
<div id='mainContainer'>
</DIV>
<input type=button value='Go' onclick='createTable();'>
</BODY>
</HTML>

Answer №1

For more information, you can visit http://msdn.microsoft.com/en-us/library/ms532998(VS.85).aspx#TOM_DOM. As Crescent Fresh pointed out in the comments, Internet Explorer requires the tbody element to be added to the table when manipulating the DOM:

Please note: In order to properly use the DOM in Internet Explorer, you must create a tBody element and insert it into the table. Unlike in HTML where the tbody is automatically implied, IE does not create it when directly manipulating the document tree.

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

Neither of the squares are appearing on the canvas

Here is the code snippet I'm working with: const target = document.getElementById("target"); const context = target.getContext("2d"); function drawSquare() { context.fillStyle = "rgb(200, 0, 0)"; co ...

dynamic rendering in React based on the value passed through props

I am attempting to render a component using react.lazy, where the path is a variable in my props. However, I am encountering an error with webpack. The parent component sends the props like this: <DynamicModal url = 'Impuesto/formulario&apo ...

What is the best way to duplicate a text using a button in ReactJS?

Hey there, I'm working with an array and trying to figure out how to copy the text in a p element ({item.adress} => this is part of an array item) when a button is clicked. Could you lend me a hand? import classes from './CryptoBox.module.css& ...

Submitting a form using an anchor tag in Angular 8: A step-by-step guide

I have a question about how to submit form data using hidden input fields when a user clicks on an <a> tag. <form action="/submit/form/link"> <input type="hidden" [attr.value]="orderNumber.id" /> <input type="hidden" [attr.value]= ...

Steps for resolving the problem: [$sce:itype] while using AngularJS version 1.6

I'm encountering an issue with $sce while using AngularJS 1.6.4: Error: [$sce:itype] http://errors.angularjs.org/1.6.4/$sce/itype?p0=html Stack trace : L/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:6:425 trustA ...

What is the method for configuring my bot to forward all logs to a specific channel?

const logsChannel = message.guild.channels.cache.find(channel => channel.name === 'logs'); I am looking to set up my bot to send log messages for various events, like member join/leave or message deletion, specifically in a channel named &apo ...

Is it possible to send the value of "this" as an argument to a different function in JavaScript?

I currently have the following code: $('#slider li').click(function () { var stepClicked = $(this).index(); alert(stepClicked); if (stepClicked != 0) { $('#cs_previous').removeClass('cs_hideMe'); } els ...

Collect all the attribute values of the checkboxes that have been checked and convert them

Is there a way to retrieve the attribute values of all checked checkboxes as an XML string? <input type="checkbox" id="chkDocId1" myattribute="myval1"/> <input type="checkbox" id="chkDocId2" myattribute="myval43"/> <input type="checkbox ...

php utilizing javascript to generate encrypted data for a hidden file

Within my MVC application, I have implemented Raty for rating images. Below is the code snippet: <div class="container"> <form method="post" class='form' role='form' action="?section=photo&view=addVote"> <input t ...

What is the process for altering a variable within an Ajax function?

Scenario: I'm dealing with JSON data fetched from the backend which needs to be presented in a table format. To achieve this, I've created a string called tableOutputString and am populating it by iterating through the JSON response. Finally, I&a ...

The resolvers contain the Query.Mutation but it is not specified in the schema

const { ApolloServer, gql } = require('apollo-server-express'); const express = require('express'); const port = process.env.PORT || 4000; const notes = [ { id: '1', content: 'This is a note', author: 'Adam ...

Errors found during compilation when running the npm start command

After choosing to develop an app with React using VS Code, I initiated the process by running npm create-react-app ./, which was a success. However, when I proceeded to execute the command npm start, it resulted in compilation errors related to files suc ...

Encountering an issue with blogs.map function in a Next.js application

import Head from 'next/head' import { useState } from 'react' import Image from 'next/image' import styles from '../styles/Home.module.css' const Home = (props) => { const [blogs, setblogs] = useState(props.dat ...

Adding a proptype for a setState method in React components

I am currently developing a reusable component for a project. This particular component includes user and setUser as props, much like the example provided below. const UserComponent = ({ user, setUser }) => { const calcHandler = useCallback(() =& ...

Are there drawbacks to utilizing large integer values as array indexes?

Simply put. We are discussing javascript here. I am wondering about the potential performance drawbacks, memory issues, and so on when using high value integers as array indexes instead of low value ones. Some programming languages allocate enough memory ...

What's the deal with Django and JSON using both single and double quotes?

Observing that when using the simplejson function in Django, all the strings are enclosed in single quotes and the entire JSON object string is enclosed in double quotes. However, when passing this string to JSON.parse, an error occurs because the standard ...

Posting a JavaScript string to a C# backend in ASP.NET Core MVC: A step-by-step guide

I am a beginner in ASP and facing an issue while attempting to pass a string from my JavaScript code to my controller. The intention is to utilize this string for querying my database. JavaScript function findEmployees(userCounty) { $.ajax({ t ...

Handling error messages with Django and Ajax

Is there a way to display error messages with Django? Here is how I am currently thinking of handling it (using jsonresponse as an example): def test(request): if request.method == "POST": if form.is_valid(): form.save return JsonResponse( ...

Combine loop results into a string using jQuery

When using jQuery, I need to append a multiline string to an element by adding a string return from each value in a for loop. $("songs-table-tr").append('tr id="songs-table-tr'+count+'" style="display: none">\ ...

Exploring an object to retrieve values based on keys using javascript

Currently, I have an array of objects set up like this: https://i.sstatic.net/l6d3z.png My goal is to loop through the array and extract users based on a specific roomCode So, I've attempted two different approaches: for(let i=0; i <data. ...