Mark
Mark

Reputation: 1872

I need to pull the ID of the 2nd from last element in jQuery

I need to pull, break down into an integer, and store in jQuery the second from last element with the class ".admin-product".

The id will be something like "admin-42" and I need that stored as the number 42.

The stored variable is sent through my AJAX handler and will be manipulated and put to use from there.

Here's my current code:

$(document).on('click', '.create-btn', function() {

var data = {'id':$('.admin-product:last').attr('id'),
           'username':$('#ausername').val(), 
           'email':$('#aemail').val(), 
           'password':$('#apassword').val()};

ShowCreateLoadingScreen("Creating...");
AjaxHandler('library/ajax/ajax.admin-account-create.php', data, 'POST', true);

});

Any ideas?

EDIT:

Preferrably in this format, ish:

$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);

EDIT:

The following code stores the number as 2.

{'id':$('.admin-product:nth-child(-n+2)').attr('id').split("-")[1]

EDIT:

My mark-up is generated through a parser, the code it is here, basically rewrites the same line for as many times as there is still information in my database.

if($stmt->num_rows > 0) {
    $stmt->bind_result($aId, $aUsername, $aPassword, $aEmail);
    while($stmt->fetch()) {
        $html .= file_get_contents(ROOT_LIB . 'html/admin-accounts/row-user.html');
        $rowNumber = $aId;

        $replace = array(
        'userHere'      => $aUsername,
        'emailHere'     => $aEmail,
        'passHere'  => ' ',
        'IdHere'    => $aId,
        'buttonDisplay' => 
        '<button type="button" data-id="'.$aId.'" name="edit" class="btn btn-info edit-product span6" title="Edit Account" value="Edit">Edit</button>
         <button type="button" data-id="'.$aId.'" name="delete" class="btn delete-btn btn-danger span6" title="Delete Account" value="Delete">Delete</button>'
    );

    $parser = new Parser($replace);
    $parser->ParseHtml($html);
}

Upvotes: 1

Views: 81

Answers (3)

Naveen Chandra Tiwari
Naveen Chandra Tiwari

Reputation: 5133

Give this a try:

 var ele = $(".admin-product").length;
 var id = $(".admin-product:eq(" + (ele - 3) + ")").attr('id').split('-')[1];
 Let me know if it doesn't work.

Upvotes: 0

T J
T J

Reputation: 43166

this is a very basic ancient way:

var elements = $('.admin-product');
var len= elements.length;
var element = elements[len-2];
var data= $(element).attr('id');
var id= data.split('-')[1];

You can simply do

var id = $('.admin-product:nth-last-child(2)').attr('id').split('-')[1];

update: fiddle

Upvotes: 1

Ethan Brown
Ethan Brown

Reputation: 27292

It sounds like what you're interested in is the ID number of some DOM elements; in this case, the "ID number" is the suffix of the HTML ID of the element. So what I would do is construct a list of said ID numbers:

var idNums = $('.admin-product').toArray()
    .map(function(domElt){
        return Number(domElt.id.split('-')[1]);
    });

Note that if there are any elements with class admin-product that don't have a properly formatted ID, it will result in an element with a value of NaN; you can use Array.prototype.filter to get rid of those if you wish.

Then its easy to get the penultimate (second-to-last) ID (with a safety in case there's only one element):

var penultimateIdNum = idNums.length>1 ? idNums[idNums.length-2] : null;

Demonstration: http://jsfiddle.net/3SvxB/

Upvotes: 1

Related Questions