1

I need to store event target elements in db/localstorage, on reload I need to update their properties based on changes. For example: There is red color text originally, but on click, need to change its property to blue color. On reload, blue color should replace the red color from Javascript (jQuery). The issue is, we can't guarantee if the element has id attribute. How can we store element unique identifier in db and later use it for updates?

$("body *").click(function (evt) {
    console.log($(this), 'this');
    console.log($(evt.target), 'evt target', $(evt.target)[0]);
    $(evt.target).css('color', 'blue');
    //storing event.target in local storage here. with changed property
}

$("body").ready(function() {
    //retrieve elements from db.
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
anilCSE
  • 2,461
  • 3
  • 23
  • 29
  • 2
    `evt.target.id` would suffice – Satpal Feb 20 '17 at 12:05
  • We cannot guarantee if all elements has "id" attribute – anilCSE Feb 20 '17 at 12:06
  • 2
    So you want a unique identifier to an element that may or may not have a unique identifier? – BenM Feb 20 '17 at 12:08
  • What properties/attribute will be unique and guaranteed? – Satpal Feb 20 '17 at 12:08
  • _"How can we store element unique identifier in db and later use it for updates"_. If the element doesn't have an id, then by definition you can't store the id. Surely this is trivially obvious. Either give all the elements ids, or find some other combination of things by which to reliably uniquely identify it (e.g. CSS properties, position in DOM, or something.). – ADyson Feb 20 '17 at 12:08
  • Actually the requirement is to build for all possible cases. The program should work on any page (Any client who uses our SDK). – anilCSE Feb 20 '17 at 12:09
  • 1
    This sounds like a line in your docs - "To use our cool SDK, all of your elements must be uniquely identifiable...." – Jamiec Feb 20 '17 at 12:11
  • 1
    @Jamiec Or "Our cool SDK only works on uniquely identifiable elements". :D – Suresh Atta Feb 20 '17 at 12:13
  • Related (if not complete duplicate): http://stackoverflow.com/questions/226689/unique-element-id-even-if-element-doesnt-have-one – Jamiec Feb 20 '17 at 12:14

2 Answers2

1

The issue is, we can't guarantee if the element has id attribute.

The program should work on any page (Any client who uses our SDK).

No. Simply you cannot. The only way you can identify the element is with ID or some sort of unique/group identifier. If you cannot guarantee them, there is no way you can recognise them.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

I found a way to achieve this using https://github.com/fczbkk/css-selector-generator

anilCSE
  • 2,461
  • 3
  • 23
  • 29