+ 1

Cannot copy elements properly

I have copied the element but when I click on the 'edit' button after that in the new cell I cannot edit it. Please tell me my mistake. Link: https://code.sololearn.com/WegpMUHfkYV7/#html

31st Jul 2020, 6:08 PM
Shivank
Shivank - avatar
11 Antworten
+ 1
Jquery listeners wont work when tied directly to dynamic content. To get around this you tie it to a static element and within that element only look at click on your selector $('staticElem').on('click', '#yourSelector', function() { //do something });
31st Jul 2020, 9:20 PM
JME
0
Could explain the code rather? I can't understand the staticElem and yourSelector part
1st Aug 2020, 3:44 AM
Shivank
Shivank - avatar
0
$("table").on("click","#edit",function(){}
1st Aug 2020, 5:41 AM
JME
0
A static element is one that's always part of the document, it is never created or destroyed by jquery. yourSelector is just whatever element you are trying to detect clicks on
1st Aug 2020, 6:11 AM
JME
0
Thank you for the tip. I tried it out but when I clicked on the edit button of the new row the text of the first row was changing. Is there a way to fix that?
1st Aug 2020, 7:50 AM
Shivank
Shivank - avatar
0
Yes, that issue is because you are always targeting the same element by id, you need to change your target based on which edit button is clicked
1st Aug 2020, 7:53 AM
JME
0
Give each element you create unique ids. The you can get the event.target from jquery. $("table").on("click","#edit", function(event){ var elem = event.target.id } And based on which button is clicked use that to target the text you want to change
1st Aug 2020, 7:57 AM
JME
0
So could you please give the code for the edit button.
1st Aug 2020, 8:48 AM
Shivank
Shivank - avatar
0
I did some research and I found out that id name cannot be repeated more than once so I changed it to all the IDs to class but when I click on the edit button both the rows content changes. Is there a way to fix it?Is there a way to change the cell content based on which cell button was pressed?
1st Aug 2020, 10:39 AM
Shivank
Shivank - avatar
0
when you create a new row, you should add an itterator variable to the id's so instead of #edit and #celltext you would have #edit1 then #edit2… and so based on which edit id is returned you just have to slice off the number at the end and use it to target the corresponding #celltext + itterator
1st Aug 2020, 3:51 PM
JME
0
Var itterator = 1 append("<tr class='row'><td class='column'><p id='celltext"+itterator+"'>New</p><button id='edit"+itterator+"'>edit</button><button id='addrw'>add row</button><button id='addclmn'>add column</button></td></tr>"); Itterator++
1st Aug 2020, 3:55 PM
JME