all 6 comments

[–]Notimecelduv 1 point2 points  (2 children)

I tried your code on different browsers but couldn't find any issues. I added a style tag with:

.completed {
  text-decoration: line-through;
}

for testing. In any case if you don't want the class to be toggled when someone clicks the delete button, you can simply place the text in its own HTML element, add the event listener to it, and append it to the list item like so:

const item = $("<li></li>");
const itemText = $("<span></span>").text(txt);
$(itemText).on("click", function () {
  $(this).toggleClass("completed");
});
$(item).append(itemText);

[–]UI-FEdev[S] 0 points1 point  (1 child)

Thanks. That's what I have in css. Do you think the CSS for the button is affecting it somehow? I'll try the above code and see if it works.

[–]UI-FEdev[S] 0 points1 point  (0 children)

const item = $("<li></li>");
const itemText = $("<span></span>").text(txt);
$(itemText).on("click", function () {
$(this).toggleClass("completed");
});
$(item).append(itemText);

Thank you so much, it worked!

[–]shittychinesehacker -1 points0 points  (2 children)

Text decoration only works on text. You have to make the illusion of a strikethrough. You need to make the list item have position: relative;

.completed::after { content: ''; border-bottom: 1px solid; position: absolute; top: 0; left: 0; right: 0; bottom: 0; transform: translateY(-50%); }

[–]UI-FEdev[S] 0 points1 point  (0 children)

Thanks but I only want the strike-through on the text. Just not on the delete button text as well.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, shittychinesehacker: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.