The input element is not assigning an ID or class

My MySQL query generates input rows in a table, allowing users to edit them. However, I want users to be able to add new rows as well. To achieve this, I created a JavaScript function that adds a new row when a button is clicked. The issue I'm facing is that the new input tag is not receiving its id or class.

function addrow(id) {
  var tableref = document.getElementById(id);
  var nextseq  = document.getElementById('maxseq').value;

  var newRow = tableref.insertRow(2);
  var rowid  = "tr"+nextseq;
  
//first cell
 var cell1 = newRow.insertCell(0) ;
 var text1 = document.createTextNode(nextseq);
 cell1.appendChild(text1);
 var id1   = "cell" + nextseq + "1"; 
 cell1.id = id1;

//second cell
var id2   = "cell" + nextseq + "2";             
var cell2 = newRow.insertCell(1).innerHTML = '<input type = "text" id = id2 class = "xxx"    style = "width:100px" value = "1001-01-01" >';


    ...more functionality that is working correctly ...
 }

When I alert on "test," it returns 'test is null,' indicating that the id is not being set.

I also attempted

var abc = document.getElementsByClassName("xxx");
alert("abc is " + abc); 

The alert displays "abc is [object HTMLCollection]." How can I resolve this issue and ensure that the id and class work as intended?

Answer №1

To properly utilize id2 as a variable, it is recommended to use it in the following manner:

 let cell2 = newRow.insertCell(1).innerHTML = '<input type="text" id="' + id2 + '" class="xxx" style="width:100px" value="1001-01-01">';

Answer №2

To create a combined string, you will have to concatenate the variables in JavaScript as it does not support string interpolation.

var id2 = "cell" + nextseq + "2";             
var cell2 = newRow.insertCell(1).innerHTML = '<input type = "text" id = ' + id2 + ' class = "xxx" style = "width:100px" value = "1001-01-01" >';

However, please note that cell2 stores the string and not the new node. A cleaner alternative approach would be:

var cell2 = newRow.insertCell(1);
var input = cell2.appendChild(document.createElement('input'));
input.id = "cell" + nextseq + "2";
input.className = "xxx";
input.value = "1001-01-01";

I suggest moving the width:100px; styling information to CSS instead of directly on the element. But if it has to be there, you can use:

input.style.width = "100px";

The alert() function shows [HTMLCollection] because that's the output returned by getElementsByClassName(). It depends on what specific output you were expecting to see.

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

Incorporate the AngularJS controller into your JavaScript code

I am facing an issue with my jQuery UI dialog that contains a dynamic <select> populated with Angular and AJAX. The problem is that the AngularJS script still runs even when the dialog is not visible. To solve this, I added a condition to stop the s ...

AngularJS $scope.$watch doesn't detect changes when the input box is cleared

Recently, I delved into AngularJS and encountered a challenge while attempting to share data between two ng-controllers within the same ng-app module using a factory. Surprisingly, the data (HTML input field) from controller1 mostly gets shared with contro ...

The component next/image is experiencing issues when used in conjunction with CSS

I struggled to create a border around an image because the custom CSS I wrote was being overridden by the Image component's CSS. Despite trying to leverage Tailwind and Bootstrap to solve the problem, my efforts were unsuccessful. Now, I am at a loss ...

Using a loop in a Vue.js slick slider: easy step-by-step guide

While utilizing vue-slick link https://www.npmjs.com/package/vue-slick within a bootstrap modal, I encountered an issue when using a v-for loop to iterate through the items. The problem can be seen in this example: https://i.sstatic.net/PhJCE.jpg. Below i ...

What is the best way for Flask to host the React public files?

When working with React, I created a folder called ./public/assets, and placed an image inside it. Running npm start worked perfectly fine for me. However, after running npm run build in React, I ended up with a ./build folder. To solve this issue, I moved ...

Trimming Picture on User's Device

Currently, I am utilizing the jQuery ImgAreaSelect 0.9.10 plugin to crop images that are uploaded by users. The data input format being used is "multipart/form-data". Upon cropping an image with the plugin, it provides me with coordinates in pixels. Howev ...

Retrieving the output value from a callback function in Sqlite3

In my current project, I am using Sqlite3 with an Express backend along with a React frontend. My goal is to verify if a user with a specific email exists in the database. While working on the function provided below, which is still a work in progress, I e ...

Trying to retrieve a CSS property using jQuery

Having trouble retrieving the correct CSS value for a background using a spectrum.js color picker. No matter which color is chosen, rgba(0,0,0,0) is always selected. Strangely enough, when checking the background in the console, it shows up correctly in th ...

Redirecting the socket.io client to the Heroku service

Recently, I developed a real-time chat application using socket.io, Node.JS, and express. It was functional on my local server but now I want to connect it to an existing Heroku service instead. How can I achieve this? Once I tried the following code, va ...

Using Javascript/Ajax to manually delete an event handler from a Sys.EventHandlerList() can be achieved by following these

I have encountered a situation where I am working with two script controls, one containing the other. Successfully, I have managed to handle events from the child control on the parent using the following code snippet: initialize: function() { this._ ...

Managing Actions in React-Redux: Understanding the Dispatch Function

While I am delving into the world of React, I stumbled upon an example that looks like this: //index.js const store = createStore(reducer) render( <Provider store={store}> <AddTodo /> </Provider>, document.getElementById(' ...

Determine the width of the uploaded image upon completion of the upload process with the help of the

After uploading an image using uploadify, I need to determine its width so that I can ensure it maintains the correct proportions with the previous one. OBJECTIVE: Determine the WIDTH of the uploaded image ...

Utilizing getServerSideProps in the new app router (app/blah/page.tsx) for maximum functionality

I am a beginner in Next.js and I am currently experimenting with the new app router feature by placing my pages under app/.../page.tsx The code snippet provided works when using the page router (pages/blah.tsx) but encounters issues when used in app/blah/ ...

Struggling to get SmartWizard Jquery 4 to integrate smoothly with Bootstrap 4

Greetings, StackOverflow community! I am a newcomer here seeking assistance with fixing my code. I am struggling to get SmartWizard jQuery 4 to function properly. Despite meticulously copying every line of code (excluding css and js file sources), the plug ...

How to Programmatically Disable OnClick Functionality in a Nested Div Using JavaScript and jQuery

I'm currently working on a Javascript application that allows users to enable/disable controls dynamically. While I've had success in disabling/enabling nested inputs and buttons, I'm facing an issue with disabling an onclick event within a ...

IE8 is not properly handling nested HTML elements that have the display:none property set

I am currently using jQuery to create a smooth fade-in effect for an <article> element that contains a <section> element. The outer element has CSS properties of display:none, position:fixed, and z-index:5. Meanwhile, the inner element is styl ...

Issue: Module - webpack-dev-server.js cannot be located

I'm in the process of creating a React app from scratch. Typically, I use npm create-react-app which sets everything up for you automatically. Following this tutorial https://www.youtube.com/watch?v=deyxI-6C2u4&ab_channel=TraversyMedia has been he ...

Solving the Dilemma of Ordering Table Rows by Value in JavaScript

I am currently working on a table and my goal is to display rows in an orderly manner based on the sum of their columns. Essentially, I want the rows with the highest values to be displayed first, followed by rows with second highest values, and so on. Des ...

Cufon: Hovering on links causes them to resize and remain in that new size

I've encountered an issue with text links in a paragraph that are being replaced using Cufon. When I hover over the links, the text inside them expands and remains that way even after moving the cursor away. The color change applied to the hover state ...

Failure to retrieve data with ajax call in php script

I am a beginner in the world of JS and PHP. I am taking on the challenge of teaching myself. Following the guidance provided in this answer from Stack Overflow, I attempted to implement it in my project. Unfortunately, I am facing challenges and would gre ...