jQuery element.closest(...).attr is not a function when using each

The Whiz of Oz

When iterating over some DOM elements I found it impossible to use .data or .attr on them:

$('.running').each (index, element) =>
    console.log element.closest('[data-id]')

gets me

<section class="job-block" data-id="240"></section>
...

but

$('.running').each (index, element) =>
    console.log element.closest('[data-id]').data('id')

throws

Uncaught TypeError: element.closest(...).data is not a function

Pranav C Balan

The closest() method that you are using is native JS method and which returns DOM element object since element refers DOM element object.


There are several options to get the attribute value, either get from dataset property :

$('.running').each (index, element) =>
    console.log element.closest('[data-id]').dataset.id


Or wrap element by jQuery and use data() method.

$('.running').each (index, element) =>
    console.log $(element.closest('[data-id]')).data('id')


Or wrap the element by jQuery and use jQuery closest() method.

$('.running').each (index, element) =>
    console.log $(element).closest('[data-id]').data('id')

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

counting no of specific rows in a table using jquery(.each() function) also using .attr() function

分類Dev

jQuery element.closest(…).attr は関数ではありません

分類Dev

Get closest image attr("src") jQuery

分類Dev

How to get the closest ul element to the given css offset using jquery

分類Dev

jQuery element.closest(...)。attrは、それぞれを使用する場合の関数ではありません

分類Dev

Using function .when jQuery in "Javascript"

分類Dev

jQueryを理解する$(this).closest(element);

分類Dev

Find closest hidden element and show it with jquery

分類Dev

Jquery - get element closest to clicked button in container

分類Dev

jQuery closest() not working on next element in hierarchy

分類Dev

Do you need to always hide the element when using jQuery slideDown function?

分類Dev

actionMenuTextColor not working when using ?/attr

分類Dev

Jquery - appending each element

分類Dev

How to use a scipy function on each element of a tensor using Keras?

分類Dev

Protractor stale element reference when using the each() method

分類Dev

jQuery traversal of HTML using .parent() instead of .closest()

分類Dev

jQuery fire function when element specific class change

分類Dev

jQuery 'each' function and 'this' variable

分類Dev

jquery each as'named function '

分類Dev

jQuery Loop by id for each element

分類Dev

JQuery .attr();

分類Dev

Add function to a jQuery element

分類Dev

Any Ideas how to iterate an element in the each loop in jQuery from within a click function within it?

分類Dev

How to override the effect when the function display() is predefined AND change the effect in each element of the array

分類Dev

jquery .attr(attributeName、function)と同等のVanilla js

分類Dev

How to traverse next/Previous 3 element in a list using Jquery each() method

分類Dev

dojo closest on clear element

分類Dev

How to submit attr value and form values with POST using JQuery and PHP?

分類Dev

jQuery "Uncaught TypeError:undefined is not a function" when using fadeIn();

Related 関連記事

  1. 1

    counting no of specific rows in a table using jquery(.each() function) also using .attr() function

  2. 2

    jQuery element.closest(…).attr は関数ではありません

  3. 3

    Get closest image attr("src") jQuery

  4. 4

    How to get the closest ul element to the given css offset using jquery

  5. 5

    jQuery element.closest(...)。attrは、それぞれを使用する場合の関数ではありません

  6. 6

    Using function .when jQuery in "Javascript"

  7. 7

    jQueryを理解する$(this).closest(element);

  8. 8

    Find closest hidden element and show it with jquery

  9. 9

    Jquery - get element closest to clicked button in container

  10. 10

    jQuery closest() not working on next element in hierarchy

  11. 11

    Do you need to always hide the element when using jQuery slideDown function?

  12. 12

    actionMenuTextColor not working when using ?/attr

  13. 13

    Jquery - appending each element

  14. 14

    How to use a scipy function on each element of a tensor using Keras?

  15. 15

    Protractor stale element reference when using the each() method

  16. 16

    jQuery traversal of HTML using .parent() instead of .closest()

  17. 17

    jQuery fire function when element specific class change

  18. 18

    jQuery 'each' function and 'this' variable

  19. 19

    jquery each as'named function '

  20. 20

    jQuery Loop by id for each element

  21. 21

    JQuery .attr();

  22. 22

    Add function to a jQuery element

  23. 23

    Any Ideas how to iterate an element in the each loop in jQuery from within a click function within it?

  24. 24

    How to override the effect when the function display() is predefined AND change the effect in each element of the array

  25. 25

    jquery .attr(attributeName、function)と同等のVanilla js

  26. 26

    How to traverse next/Previous 3 element in a list using Jquery each() method

  27. 27

    dojo closest on clear element

  28. 28

    How to submit attr value and form values with POST using JQuery and PHP?

  29. 29

    jQuery "Uncaught TypeError:undefined is not a function" when using fadeIn();

ホットタグ

アーカイブ