My question is more regarding the best way/practice how to realise one thing. Lets assume I have a possibility to create a html code for JQuery to retrieve some text value. What is the best way to do it?
I know you can just get a value using the get by id and so on, so basically I could create my own div with unique ID and by JQuery retrieve some value in it. But is this the good way? Of course another thing is that the value should not be seen by the people.
Maybe I should put the value as an attribute? Key, value pair? And get element by id and get the attribute?
So any suggestions, the best way to store one value on the page and easily retrieve it?
Thanks in advance
An update:
An example:
I do something as following:
<div id="uniqueID" style="display:none"> Value which in need </div>
This is some solution, then i just need to retrieve this value with JQuery, simple.
But what i whant to know is there a better way, maybe there is some special tag in HTML, or something that other people prefer, etc …
By jquery i just say something like:
$("#uniqueId").val()
And there should not be anything more complicated queiet simple, didn’t know it could be so unclear.
If you just want your server to embed a value in your page that is not visible to the viewer that you can then retrieve in javascript, you might as well just have your server put the value in a javascript variable directly. There is no reason to put it in the DOM if you just need it in javascript.
For example, you can just put this in your page:
Then, you can just access
myValueSetByServeranywhere in the page javascript. There is no need to put it in a div or on an attribute or anything like that.If you were going to have lots of these, you could just define an object and make your values all be properties on the object:
Of course, there are many other ways to store things in the actual page like you have mentioned including a data attribute on a DOM node, the text value of a hidden DOM node, etc… but none are as simple as just putting the value in a javscript variable if you just want to use the value from javascript.