1

EDIT

why i try to achieve this ?

Encapsulation purpose. generally every programmer encapsulate his/her code. also think about encrypt url or slug url and programmer also use uuid (Universally unique identifier) instead of id (integer id).

END EDIT

I set data attribute as a id to my html element and when I console log or debug html element I can see the data id (1,2)....

I want if anyone debug my html he can not see my data id(1,2) ...

how I will do it invisible or unseeable ? or is there is a way do it invisible or unseeable ?

below example..

 const list = document.querySelectorAll('.item');
        for(let x = 0; x < list.length; x++){
            // list[x].setAttribute('data-id',(x+1));
            list[x].dataset.id = x+1;
            console.log( list[x]);
            list[x].addEventListener('click',goCategoryPage)
        }
       function goCategoryPage(e){
           e.preventDefault()
          //    
       }
<ul class="list">
        <li class="item">Category 1</li>
        <li class="item">Category  2</li>
    </ul>
نور
  • 1,425
  • 2
  • 22
  • 38
  • What do you mean by invisible? Maybe you could add the input html and the expected output html. – htho Oct 02 '20 at 09:27
  • @htho that means when you console log or debug the element you can not see the data id (1,2)... don't know how to better explain... – نور Oct 02 '20 at 09:34
  • What is the purpose of hiding this information? Who is it being hidden from? – Kevin B Oct 13 '20 at 18:07
  • @Kevin B why uuid (Universally unique identifier) is invented.. why programmer are using uuid instead of id (like integer).. why are you asking like this way ? – نور Oct 13 '20 at 18:31
  • Because nothing on the client is truely hidden. If you're trying to hide it from people who know how to inspect element... chances are they know how to look at your code and insert a breakpoint too. This is most likely a useless increase in complexity for no gain. – Kevin B Oct 13 '20 at 18:31
  • @Kevin B people do his best effort. every programmer try to hide or secure data. if someone try to good what is wrong with that ? – نور Oct 13 '20 at 18:51
  • it isn't real security. What good is it? it does nothing to protect the data. – Kevin B Oct 13 '20 at 18:52
  • @Kevin B why programmer encrypt URL ? why use slug url ? maybe you right ... – نور Oct 13 '20 at 18:54

1 Answers1

3

The short and simple Answer is: Use something like list[x].myId = x+1; To set a custom property. As opposed to attributes, properties are not reflected in the DOM.

This gets the job done. But is considered bad practice. Because there are some caveats:

  1. Your custom property today, might be a standard property in the future, but with a completely different meaning. To make sure this won't become an issue in the future, make sure to add a prefix like your apps name or at least "my" to your property.
  2. It is okay to store primitive data like numbers, booleans or strings this way. But you might get issues with memory leaks if you start storing more complex data. Especially if you start to store references to other DOM Nodes.

More details on the topic of custom properties are discussed here: Can I add arbitrary properties to DOM objects? I think it is a question of personal taste and you really need to make sure you don't produce (future) name collisions and don't store complex data.

For primitive data like your id, I don't see a reason not to store it as a data attribute. There should be nothing to be ashamed of. It also is quite hard to keep a secret in JavaScript in the Browser.

There are two other approaches I'd consider if I had to store more complex data.

  1. Use a WeakMap. You keep your WeakMap in a central place. It has the DOM Node as key and your data as Value. If the Node is garbage collected your data is too.
  2. Create a new function (closure) for each node, which contains the ID and attach it to the click event of your Node. Event listeners are also garbage collected.

Here details on the closure approach:

const list = document.querySelectorAll('.item');
for(let x = 0; x < list.length; x++){
    console.log(list[x]);
    list[x].addEventListener('click', function () {
        const id = x+1;
        e.preventDefault();
        // stuff to be done on click
    });
}

For each iteration of the loop, a new function (with a new closure) is created. Inside this closure the value of x is the value of x in the current iteration - where the function is created.

htho
  • 1,549
  • 1
  • 12
  • 33
  • firstly thanks for your response ...I am acquaintance with closure. would you give some time for make an example for second (Create a new function (closure) ) approach ? actually i did not figure it out second approach ..and have any doc or source about set a custom property `list[x].mYId = x+1;` ? – نور Oct 04 '20 at 05:23
  • 1
    @noor I added more information on closures and custom properties. – htho Oct 05 '20 at 07:24
  • thanks again... possibly i will accept your answer on time... – نور Oct 05 '20 at 08:21
  • @noor too bad I didn't get the bounty. I thought my answer was sufficient after my edit. – htho Oct 13 '20 at 16:44
  • I don't know what really happened .. I accept your answer.. – نور Oct 13 '20 at 17:00
  • I am also confused how bounty are awarded ? what is the rules ? or how it's work ? I am disappointed also.. sorry.. – نور Oct 13 '20 at 17:05
  • it was my fault .. i did not know about this ... i asked about this on OS Meta.. please cheek the [link](https://meta.stackoverflow.com/questions/402022/why-wasn-t-the-bounty-awarded-to-this-accepted-upvoted-answer) – نور Oct 13 '20 at 19:08