0

I'm coding little CMS for translations of static pages to many languages. It dynamically refreshes and loads translations etc, but I've got a few bugs which I cannot fix. One of them is when you try to preview page of id 1, but before you were editing page of id 2, it always redirects you to preview of page of id 2.

That's my button in template:

<div class="edit-template-container">
   //other code
   <button id="a-preview" href="#" class="btn btn-block" target='_blank'>
      Preview template
   </button>
</div>

And it's function to load preview in new window:

$('#a-preview').on('click', function () {
    var pageId = $('#language-choice-select').data('page-id');
    console.log(pageId);
    window.open('/main/staticPages&staticPageId=' + pageId, '_blank');
});

Then I've read some articles about event-delegation and assumed it could be my problem so changed that code to:

$('.edit-template-container').on('click', '#a-preview', function () {
    var pageId = $('#language-choice-select').data('page-id');
    console.log(pageId);
    window.open('/main/staticPages&staticPageId=' + pageId, '_blank');
});

But it stills redirects to page of id 2 if it was edited before. In DevTools Elements I can see that data-page-id is changed, but console.log always prints for example id: "2" if it was edited before.

Data 'page-id' attribute is changed when I choose page to edit in table, on function which looks like that:

$('table').on('click', '.edit-table-btn', function () {
    var pageId = $(this).data("page-id");
    //the rest of code
    $('#language-choice-select').attr('data-page-id', pageId);
});

What should I do to change page-id data-attribute dynamically in DOM? What should I know about this, are there any fine articles?

Reporter
  • 3,897
  • 5
  • 33
  • 47
TheDraom
  • 1
  • 2
  • 3

1 Answers1

1

Please refer to this existing post:

jQuery Data vs Attr?

Basically, when you use data() on a node, jQuery sets the value on the node object, not on the DOM element. So, I suggest you to use attr() both to set and to read the value.

Community
  • 1
  • 1