jQuery - Find .attr of the last-child element

Анжел Миленов

I'm trying to get by jQuery the .attr("msg_g") of the last-child element

<div id="append">
  <div class="h" msg_g="1">A</div>
  <div class="y" msg_g="2">B</div>
  <div class="h" msg_g="3">C</div>
</div>

I'm pretty sure that script below must work but returns me undefined

var msg_g = $( "#append div:last-child" ).attr( "msg_g" );
console.log(msg_g);

I use the script below for checking if last-child 'hasClass' and works perfect, why doesn't work on .attr()

Example for .hasClass()

if($( "#append div:last-child" ).hasClass( "y" )){
    var last_msg_class="by_you";
}else if($( "#append div:last-child" ).hasClass( "h" )){
    var last_msg_class="by_friend";
}
Kartikeya Khosla

Instead of adding msg_g attribute,try using data-* attribute.

Modify your html as :--

<div id="append">
<div class="h" data-msg_g="1">A</div>
<div class="y" data-msg_g="2">B</div>
<div class="h" data-msg_g="3">C</div>
</div>

and try this in Jquery :--

var msg_g = $( "#append > div:last" ).data( "msg_g" );

and make sure that you're Jquery code is inside document.ready block.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find last child with jquery

From Dev

Jquery find element with style attr and change it

From Dev

jQuery selector to find the last element

From Dev

jquery find child element is checkbox or not

From Dev

How to check if element is last child without JQuery

From Dev

Jquery element height as attr

From Dev

How to get last element in the .find() object by jquery

From Dev

jQuery find and replace anchor in child element

From Dev

How to find the child element on hover using jQuery?

From Dev

What should be changed about this Xpath string to find the last child element?

From Dev

:last selector doesn't find last element in jQuery

From Dev

Target the child element of a .last()

From Dev

jQuery add css class only when last child element is visible

From Dev

Find li element by its child element id in jQuery or JS

From Dev

JQuery - Unable to find element ID after child element is removed

From Dev

Find li element by its child element id in jQuery or JS

From Dev

jQuery find div with data attr

From Dev

jQuery find style attr of ID

From Dev

Using jQuery is there a way to find the farthest (deepest, or most nested) child element?

From Dev

JQuery find child element inner text and replace with IMG HTML

From Dev

Find number of second child in html element using JQuery

From Dev

jQuery : Find nth-child of a element saved as a variable

From Dev

jquery how to find nth child from element variable

From Dev

Find the last element in an array

From Dev

Select last child with specific element

From Dev

Trying to removing last child of a child element

From Dev

Append a child element before the last child

From Dev

Trying to removing last child of a child element

From Dev

jQuery - select child of child of element

Related Related

  1. 1

    Find last child with jquery

  2. 2

    Jquery find element with style attr and change it

  3. 3

    jQuery selector to find the last element

  4. 4

    jquery find child element is checkbox or not

  5. 5

    How to check if element is last child without JQuery

  6. 6

    Jquery element height as attr

  7. 7

    How to get last element in the .find() object by jquery

  8. 8

    jQuery find and replace anchor in child element

  9. 9

    How to find the child element on hover using jQuery?

  10. 10

    What should be changed about this Xpath string to find the last child element?

  11. 11

    :last selector doesn't find last element in jQuery

  12. 12

    Target the child element of a .last()

  13. 13

    jQuery add css class only when last child element is visible

  14. 14

    Find li element by its child element id in jQuery or JS

  15. 15

    JQuery - Unable to find element ID after child element is removed

  16. 16

    Find li element by its child element id in jQuery or JS

  17. 17

    jQuery find div with data attr

  18. 18

    jQuery find style attr of ID

  19. 19

    Using jQuery is there a way to find the farthest (deepest, or most nested) child element?

  20. 20

    JQuery find child element inner text and replace with IMG HTML

  21. 21

    Find number of second child in html element using JQuery

  22. 22

    jQuery : Find nth-child of a element saved as a variable

  23. 23

    jquery how to find nth child from element variable

  24. 24

    Find the last element in an array

  25. 25

    Select last child with specific element

  26. 26

    Trying to removing last child of a child element

  27. 27

    Append a child element before the last child

  28. 28

    Trying to removing last child of a child element

  29. 29

    jQuery - select child of child of element

HotTag

Archive