knot22 knot22 - 3 months ago 7
HTML Question

setting name of an HTML element using jQuery

Here is some code that is resulting in failure to set 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');
return $list;

When I open the Inspector in Firefox, right click on one of the li's, and choose 'Show DOM Properties' the className property shows empty string:
enter image description here

Since there is a CSS rule for the class test123 that says to make the font blue, and the font is still black, it appears that this line of code is not assigning a class of 'test123' to the li tags. I have also tried
$('<li/>').attr('class', 'test123');
which has given the exact same result as when trying to use .prop(). Why aren't these working?


You should try using addClass() instead of prop()

And you are creating two different list items and only appending one of them.

$('<li/>').prop('class', 'test123');

You can save try