stephenscaff / read-smore

A little Read More JS thing
https://stephenscaff.github.io/read-smore/
MIT License
28 stars 7 forks source link

Fix: remove duplicate id when called mote than one time #31

Closed mrdotb closed 1 year ago

mrdotb commented 1 year ago

Hello,

Thanks for the lib :+1: I'm using it and a website which is not static. I notice that calling ReadSmore(el).init() two time in a page create duplicate link id which break the attachment of the event listener.

I replaced the logic that rely on an id by using the internal state and nextSibling.

stephenscaff commented 1 year ago

Thanks for the idea / contribution.

I haven't pulled this down to check it out yet. But, do have 2 questions.

  1. This would be a breaking change yeah?

If so, should we offer backwards compatibility? Realize this isn't a massive lib, but, still.

  1. Could your issue be better solved on the user side? I've also used this in a non static / dynamic build. Could you simply place inside a loop / forEach? That's how I expected use.

Am I missing something? Or, is your solution better?

Will take look when I get a sec later.

Thanks!

mrdotb commented 1 year ago

1.It's not a breaking change.

2.It's not possible to solve my issue without changing the generated ID or changing the logic to find the ID.

I have the following scenario where I need to create two readsmore one after the page is loaded another one after an Ajax request. This produces two identical ID. ReadSmore(el).init() -> create a link with an ID {blockClassName}_{iterator} link_0 -> creating a new dom after Ajax req -> ReadSmore(el).init() create a link with an ID {blockClassName}_{iterator} link_0

Using the internal state to retrieve the link dom instead of an ID seems appropriate.

stephenscaff commented 1 year ago

Okay. I see now.

Makes sense.

Thanks!