Reputation: 53
Is it okay to generate HTML page like this:
...
<div id='some_id' lesson_id='313'>
...
</div>
...
And in jQuery just do
lesson_id = $("#some_id").attr("lesson_id")
// send some AJAX POST requests with this lesson_id
Is it normal way to pass some data to js from server side ?
Many thanks!
Upvotes: 3
Views: 76
Reputation: 4412
Use jQuery Data
<div id='some_id' data-lessonid='313'>
...
</div>
lesson_id = $("#some_id").data("lessonid")
Upvotes: 2
Reputation: 1785
So long as it doesn't reveal any major information that a malicious user can exploit, you can do that. As has been said, though, you should add the data-*
attribute prefix, as this allows you to have custom HTML that's still considered valid.
Upvotes: 2
Reputation: 16828
Use the data
attributes:
<div id="some_id" data-lesson_id="313">
then in jQuery get the value:
var lesson_id = $('#some_id').data('lesson_id');
More about .data()
: https://api.jquery.com/jquery.data/
Upvotes: 10