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


i need pull, break down integer, , store in jquery second last element class ".admin-product".

the id "admin-42" , need stored number 42.

the stored variable sent through ajax handler , manipulated , put use there.

here's 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 format, ish:

$id = filter_input(input_post, 'id', filter_validate_int); 

edit:

the following code stores number 2.

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

edit:

my mark-up generated through parser, code here, rewrites same line many times there still information in 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); } 

it sounds you're interested in id number of dom elements; in case, "id number" suffix of html id of element. construct list of said id numbers:

var idnums = $('.admin-product').toarray()     .map(function(domelt){         return number(domelt.id.split('-')[1]);     }); 

note if there elements class admin-product don't have formatted id, result in element value of nan; can use array.prototype.filter rid of if wish.

then easy penultimate (second-to-last) id (with safety in case there's 1 element):

var penultimateidnum = idnums.length>1 ? idnums[idnums.length-2] : null; 

demonstration: http://jsfiddle.net/3svxb/


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -