user1937021
user1937021

Reputation: 10781

if last child OR second to last child

How can I target the second to last child in this example?

if( ($(this).is(":last-child")) || ($(this).is(":eq(-2)")) ) {

:last-child works for me but not :eq(-2).

Upvotes: 0

Views: 7327

Answers (3)

Mohammed Javed
Mohammed Javed

Reputation: 876

I have try this code is working fine.

Example:

var abs = '<?php echo $this->getOrder()->getStatusLabel() ?>';
 //alert(abs);
 if(abs == "Delivery Complete"){
  //alert('Sucssess')
  var asscer = document.getElementsByClassName('form-buttons')[0].lastChild;
  asscer.style.display = "none";
  }
 if(abs == "Pending with OPS"){
  var asscer = document.getElementsByClassName('form-buttons')[0].lastChild.previousSibling;
  asscer.style.display = "none";
  //alert(asscer);

 }

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

Try this:

$(this).is(":nth-last-child(2)")

Upvotes: 1

Itay
Itay

Reputation: 16777

Try using the :nth-last-child() selector.

if ($(this).is(":last-child, :nth-last-child(2)")) 

nth-last-child selector

Description: Selects all elements that are the nth-child of their parent, counting from the last element to the first.

jQuery( ":nth-last-child(index/even/odd/equation)" )

index: The index of each child to match, starting with the last one (1), the string even or odd, or an equation ( eg. :nth-last-child(even), :nth-last-child(4n) )

Version added: 1.9

Upvotes: 5

Related Questions