skip to Main Content

I have the following problem

<a href='#' data-entry-id='{{ income.incSum }}' id='some_link_1'></a>
const $entryElements = $('[data-entry-id]');

     const $entryIds =
         $.map($entryElements, item => $(item).data('entryId'));
     console.log("entry ids ", $entryIds);

If the data-entry-id attribute is in square brackets, everything works, the values are displayed.

Only I would like to get to the value of the data-entry-id attribute whose id is some_link_1 (because I have several data-entry-id values on the page):

<a href="#" data-entry-id="{{ income.incSum }}" id="some_link_1"></a>

I use the following notation for this:

const $entryElements2 = $('#some_link_1').attr('[data-entry-id]');
const $entryIdsEle = $.map($entryElements2, item => $(item).data('entryId'));
console.log("mine is: ", $entryIdsEle);

If data-entry-id is in square brackets, the values are empty, but if it is without brackets:

const $entryElements2 = $('#some_link_1').attr('data-entry-id');

an error occurs

enter image description here

Please help me how I could get to this data using attribute and id name.

I used this article https://cruftlesscraft.com/passing-data-from-twig-to-javascript

2

Answers


  1. try something like this, that work for me

        let els = $('a[data-entry-id]');
    
        console.log(els);
        
        els.map((key, el) => {
          console.log(el);
        });
        
        let el1 = els.filter((key, el) => $(el).attr('id') === 'some_link_1');
        
        console.log(el1);
    
    Login or Signup to reply.
  2. // Only I would like to get to the value of the data-entry-id attribute whose id is some_link_1 
    
    // This selector should get the element with the id of `some_link_1` if it also has an attribute `data-entry-id`;
    
    const $entryElements3 = $('#some_link_1[data-entry-id]');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search