updating the name of an HTML element with jQuery

Encountering a problem with setting the class for each li in a group of li tags:

function pathContents(fileList)
{
var $list = $('<ul/>');
    $.each(fileList, function (_, file) {
$('<li/>').prop('class', 'test123');
$('<li/>').text(file).appendTo($list);
    });
    return $list;
}

Upon checking the Inspector in Firefox and inspecting one of the li's, the className property shows an empty string: https://i.sstatic.net/ZtMuT.jpg


Although there is a CSS rule indicating that the font should be blue when using the class test123, it remains black. It seems like the code is not assigning the class 'test123' to the li tags. Attempts such as

$('<li/>').attr('class', 'test123');
have yielded the same result as using .prop(). Why is this happening?

Answer №1

Consider using addClass() in place of prop()

https://api.jquery.com/addclass/

Also, you are creating two separate list items but only appending one of them.

$('<li/>').prop('class', 'test123');
$('<li/>').text(file).appendTo($list);

You can alternatively do

$('<li/>').addClass('test123').text(file).appendTo(list);

Answer №2

Give this a shot:

function displayFiles(fileList)
{
var $list = $('ul');
    $.each(fileList, function (_, file) {
$('li').addClass('file-item').text(file).appendTo($list);
    });
    return $list;
}

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

Using jQuery and Ajax to populate a dropdown menu with options

I'm currently working on an ajax call function that successfully fills a drop-down list with data from a specific URL. However, I am looking to modify the function so that it can receive variables in order to populate the drop-down list dynamically. ...

Is the card-columns class not available in Bootstrap 5.0? Are there any other alternatives provided by Bootstrap besides using CSS media queries and flex column?

While exploring Bootstrap 5.0, I noticed that the card-columns class was missing and there is no mention of it or its alternative in the Bootstrap 5.0 documentation. However, it can still be found in the Bootstrap 4.6 documentation. I understand that usin ...

Animating nested ng-repeat loops

Trying to animate a list of elements divided by rows (the parent ng-repeat) and columns (the child ng-repeat) has been quite challenging. While I was able to achieve the desired animation with individual ng-repeats, the same cannot be said for nested ng- ...

Decrease the size of the "subscribe" button

Our "subscribe" button is currently displayed like this: https://i.sstatic.net/toiOb.png However, we are looking to reduce the extra width and have it appear more like this: https://i.sstatic.net/NaaYJ.png We are using the same code for both versions: ...

Discover a specific segment of the pie chart

Currently, I have a pie chart that has been created using HTML5 canvas. I am able to retrieve the coordinates (X,Y) when the mouse hovers over it. Now, my goal is to determine which slice of the pie chart the Point (X,Y) falls into. Please note: I have ...

Discovering the position of elements in relation to their (grand^N)parent

Among my possessions are elements A and B. B has the ability to exist as a direct child of A, or there may be anywhere from 0 to N elements separating them. It is imperative that I determine how B is situated in relation to A, specifically noting the dist ...

Is it possible to globally delay the execution of WebElement.sendKeys() in Protractor's onPrepare function?

While running protractor on a sluggish machine, I am in need of slowing down each key press and action performed. The action part has been successfully implemented, but how can I achieve the same for key presses? I have come up with a local solution which ...

Sort users by their data attribute using jQuery

I need to sort users based on their data attribute. Within the main div called user-append, I have a variable number of users retrieved from an ajax get request. It could be 3 users or even up to 100, it's dynamic. Here is the structure with one user ...

Encountered an error "Not Found" when attempting to establish an AJAX connection between Javascript and Rails

I'm having an issue with a simple Rails controller method. When I call it from JavaScript as an AJAX method, I get the error message "Not Found" in the JavaScript error log, even though the method works fine when accessed directly in the browser. What ...

Which is more advantageous for creating a new block in Bootstrap 4 – using ".row" or ".col-12"? And what is the reasoning behind your choice?

Here is a simple example to demonstrate. <div class="container"> <div class="row"> <form class="col-12 form-group"> // some code </form> <div class="col-12"> // some text for description </d ...

Retain the chosen values even after submitting the form

Consider the following form: <form method="get" action=""> <select name="name"> <option value="a">a</option> <option value="b">b</option> </select> <select name="location"> <opt ...

Identifying an anonymous function with a name (using .name versus .displayName)

In the context of my react native project, I have come across a function with an undefined name that is not being inferred. This function looks like: const f = function() {}; Despite maintaining its anonymous definition, there is an attempt to assign a na ...

Is there a way to tally up the number of green items and display a <p> tag when every item has been marked green?

I have created a checklist that includes checkboxes. I am looking to display some text when all the checkboxes are checked and green. Can someone assist me with writing the code for this functionality? $(document).ready(function() { $("i").click(funct ...

Is it possible to apply inherited styles while using createStyles?

Here is a createStyles statement in my code: const useStyles = makeStyles((theme: Theme) => createStyles({ menuOpen: { zIndex: 1, width: 200, height: '100%', position: 'fixed', top: 48, trans ...

Using ajax to retrieve and refresh data

I need to perform real-time updates to a column in my table and display the updated data instantly. Here is the code I am using: <html> <body> <div id="output"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3. ...

Varying heights based on the screen size

Currently, I am in the process of designing my website and incorporating some wave elements to enhance the background. However, I've encountered some issues when resizing the screen. Specifically, the waves seem to shift with a space between them as t ...

Add or remove a class when a button is clicked

I am trying to achieve the functionality where upon clicking a button, one class is removed from a div and another is added. However, I have been unsuccessful so far in making it work. <div class="hiddennav displaynone"> <ul> <?php wp ...

What is the best approach to create a dynamic value from axios response in a reactive object?

I am attempting to retrieve data from the backend (specifically the user role) and store it in a reactive container with Vue: import {reactive} from "vue"; import axios from "axios"; export const store = reactive({ auth: axios.get ...

Burger menu animation activated by a single click anywhere on the page

After following a tutorial on creating an animated menu burger, I encountered a few bugs. The animation is working as intended, but it triggers no matter where I click on the page. Here is the code snippet: const toggleMenu = document.querySelector( ...

Looking to restrict the data retrieved from an API while utilizing ReactJS

I am currently fetching transaction data from an API. One of the fields in the response is buyer, which may sometimes be null. As a result, I am excluding any entries with a null buyer. This leads to varying numbers of results being displayed. My goal is t ...