Instructions for applying a border style to a dynamically generated table using jQuery

I'm currently working on adding CSS to a table that is generated dynamically when a button is clicked. The function that is triggered on the click event includes the following jQuery code to create the dynamic rows:

$("#employeDetail").append('<tr class="hide1" id="row'+empCurrentIndex+'">'
        +'<td >'+employerName[empCurrentIndex]+'</td>'
        +'<td >'+empMobileNo[empCurrentIndex]+'</td>'
        +'<td >'+emplocation[empCurrentIndex]+'</td>'
        +'<td >'+empTotNoMonth[empCurrentIndex]+'</td>'
        +'<td><button class="btn" name="delete" value="Delete" onclick="return deleteEmpRow('+empCurrentIndex+');">Delete</button></td>'
        +'<td><button class="btn" name="edit" value="Edit" onclick="return editEmpRow('+empCurrentIndex+');">Edit</button></td>'
+'</td></tr>').addClass('newRow');

The button is within the table tag and triggers this JavaScript function to create the dynamic rows. I aim to add borders to these created rows. I attempted to achieve this by using the .addClass('newRow') method:

  .newRow{
   border:5px;
   border-color:red;
  }

However, the borders don't appear to be applied. Any assistance on this matter would be greatly appreciated.

Thank you

Answer №1

Your issue stems from the fact that append() does not return the row itself, but rather the element it was appended to. To address this, one simple solution is to include the class within the class attribute when creating the row...

$("#employeDetail").append('<tr class="hide1 newRow" id="row'+empCurrentIndex+'">'
        +'<td >'+employerName[empCurrentIndex]+'</td>'
        +'<td >'+empMobileNo[empCurrentIndex]+'</td>'
        +'<td >'+emplocation[empCurrentIndex]+'</td>'
        +'<td >'+empTotNoMonth[empCurrentIndex]+'</td>'
        +'<td><button class="btn" name="delete" value="Delete" onclick="return deleteEmpRow('+empCurrentIndex+');">Delete</button></td>'
        +'<td><button class="btn" name="edit" value="Edit" onclick="return editEmpRow('+empCurrentIndex+');">Edit</button></td>'
        +'</td></tr>');

Answer №2

Regarding your CSS question, here is the correct code:

.tableRow td {
   border-bottom: 5px solid red;
}

Just to clarify, make sure you apply the tableRow class to the <td> elements, not the <table> tag. I've also used shorthand for the border style in the CSS code.

Answer №3

It seems like you are mistakenly applying the newRow class to the #employeDetail element. Is that element actually a table? You should be adding the class to the newly created row instead.

 $('#row'+empCurrentIndex+').addClass('newRow');

Give that a shot.

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

What is the best way to create a div that extends beyond the borders of the screen, but still allows only

I am currently working on a React project for a website. I am creating a category bar that should slide only the component in the mobile version, without moving the entire page. Check out the desired design here However, the current appearance is differe ...

Iteratively sift through data for boolean value

My navigation dynamically retrieves routes from the vue-router, eliminating the need for manual addition. Within these routes, there is a boolean key named "inMenu" in both parent and child routes. I have successfully filtered out the parent routes based ...

Tab buttons that switch between different sections with just a click

Forgive me if this seems like a simple coding issue, but I struggle with javascript and need some guidance... I have a setup where two buttons (blue and yellow) toggle between different content divs. On another part of the page, there are also two buttons ...

Create a loop to iterate through dates within a specified range using the Fetch API

When I need to get the exchange rate from the bank for a specific interval specified in the input, I follow these steps. The interval is defined as [startdate; enddate]. However, in order to make a successful request to the bank, the selected dates must be ...

How can I use CSS to customize the appearance of the elements within an iframe?

<iframe src="#link"> #document <!doctype html> <html> ... </html> </iframe> Is there a way to customize the styling of elements within the #document using CSS only, without needing to use Jquery? ...

Enhancing user experience with Bootstrap's body padding and customizable scrollbars

Recently, I encountered an issue with the footer color in bootstrap. To fix this problem, I created a div and removed the extra padding from the body element. You can view the code snippet here: jsfiddle.net/wctGM/9/ However, when viewing the mobile ver ...

Tips for displaying the Font Awesome icons

I have included Font Awesome icons within the <span> tags and applied the necessary styles in my CSS file. However, the icon is not being displayed correctly. I am looking for a solution to ensure the correct display of the icons. The HTML code wher ...

There seems to be a compatibility issue between AngularJS and HTML

Here is a simple AngularJS code snippet I have written: var app=angular.module("myApp",[]); app.controller("MyController",['$scope',function($scope){ $scope.name="Asim"; $scope.f1=function(){ console.log("i am clicked"); $scope.vr="lol"; } co ...

Trouble arises when trying to invoke a prototype function using setInterval

Having created a prototype class for Bot, I encountered an issue. Upon calling the init() function after its creation, it correctly alerts "a 5000". However, when the prototype function calls getUpdates(), it fails to reach the value of "this" and instead ...

Applying a condition to filter elements using AngularJS

I have created an array in my controller with the following data: var people = [ { name:"Alice", number:'808574629632', email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail ...

What is the best way to extract a single value from my directive scope?

I am working with an AngularJS directive that has a non-isolated scope, but I have one variable, "isOpen," that needs to be isolated within the directive. Consider the following example: app.directive('myDir', function() { return { r ...

Cookies are mysteriously invisible in the developer console of Safari/Chrome when using the Set-Cookie Header, yet they miraculously appear in server logs

Storing cookies for my web app using the 'Set-Cookie' header response from my python backend has been a challenge. https://i.stack.imgur.com/XMx35.png An ajax call on the client-end to the function is where I run into issues: https://i.stack.im ...

Tips for replacing all occurrences of a specific string in HTML while preserving JavaScript attributes

Is there a way to use RegEx to replace part of an HTML document without affecting the root element or other elements like event listeners? Consider this scenario: for (; document.body.innerHTML.indexOf(/ea/) != -1;) { document.body.innerHTML = docu ...

After installing Ember and running the Ember server, I noticed that the node_modules directory appears to be empty. This issue is occurring on my Windows 10 x64 PC

Welcome to the command console: C:\Users\Documents\emberjs>ember server node_modules seem to be empty, consider running `npm install` Ember-cli version: 2.14.2 Node version: 6.11.2 Operating System: Windows 32-bit x64 I'm a beg ...

Experiencing difficulty in parsing the JSON document

I am struggling with reading a .JSON file (todo.json) in my Angular.Js project. The file is stored on the server, and all static files are saved within the 'public' folder of my basic Express server. Even though I can access the todo.json file di ...

When you input text into a contenteditable div, it automatically gets placed inside a span element instead of being placed

I'm having an issue with my contenteditable div. Whenever I click the button, a span is inserted into the div. However, when I start typing, the text goes inside the span instead of outside it: jQuery(document).ready(function($) { let input = $(" ...

React and Material UI collaboration causing layout problems

Seeking assistance with implementing React and Material UI in my project. My objective is to create a layout with 3 columns on desktop - one column with a radio button and label inline, and the other two columns containing select menus with option values. ...

Tips for ensuring math formulas display correctly in CKEditor

I'm currently struggling to properly display the correct format of a math formula in ckeditor. I have made numerous attempts to find a solution, but so far, none have been successful. Here is an excerpt of my code: <html> <head> <scr ...

Transform HTML or formatted text from an iPhone 4 Notes backup SQLite file into plain text

I was looking to recover some missing notes from an iTunes backup of an iPhone 4 by accessing the notes.sqlite file. After querying the table that stores the note text: select zcontent from znotebody I found that the text is in HTML format. Is there a wa ...

How can I retrieve data from local storage based on a specific key value using a query?

In order to optimize the storage of data for my app, I have successfully stored a large amount in local storage. Now, I am faced with the task of fetching data in an array but only selecting key/values that are specifically chosen, rather than all or jus ...