Skip to content
Advertisement

How to handle if a client change some data attributes from the client side?

JavaScript

I have communities list on my page and each community item has a delete button. Now when I click on a delete button I want to delete related community item.

But…

I can inspect these delete buttons from the browser and change it’s data-id value to something else(if a data-id of a delete button is 10, I can change it to 1000). Then when I click on that delete button after changing its data-id value from the client side, it will delete some other community instead correct one. (If a community exists with that changed data-id value) because buttons data-id is changed and JavaScript code takes that value to make AJAX request. I can’t stop user changing data-id from the client-side. Therefore, How can I handle this situation if user changed data attributes from client side?

Extra information

$communities is a array of community objects and each community item has a Name and ID.

Advertisement

Answer

You could read the data- prop(s) after the page loads. Store them in an array or whatever and then delete the data- prop(s) from the elements.

Be aware that validation should probably happen somewhere on the server side instead of client side

JavaScript
JavaScript

EDIT: using suggestions from comments (event delegation)

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement