There are elements with pm and further digits:

<div class="" id=""> <div class="mes " id="pm2509"> text </div> <div class="mes " id="pm2511"> text </div> <div class="mes " id="pm2513"> text </div> <div class="mes " id="pm2515"> text </div> <div id="new"></div> </div> 

You need to use jquery to find the maximum #pm, be sure to DOM. Thank.

  • what does DOM mean necessarily ? - Grundy
  • The code appears dynamically ... - skillful
  • and? if he's not there, then he won't be found :) - Grundy
  • I beg you, do not use the id for other purposes - to store information! Add the attribute data-id="2511" and store these IDs in it. sampling (or filtering) in this case will be more reliable and transparent. - Ivan Pshenitsyn
  • because it is an element identifier, not a field for storing data. in order to accomplish what you want (yes, this is real and easy) you need to take the value of the id attribute, cut off the piece and use the remaining digit. Why, if you can immediately keep this index in its pure form in a separate attribute and not interfere with it in some "id ..."? - Ivan Pshenitsyn

1 answer 1

I would suggest to correct the code on a similar

 <div class="" id=""> <div class="mes " id="pm2509" data-id="2509"> text </div> <div class="mes " id="pm2511" data-id="2511"> text </div> <div class="mes " id="pm2513" data-id="2513"> text </div> <div class="mes " id="pm2515" data-id="2515"> text </div> <div id="new"></div> </div> 

and then in js

 var $max = null; $('.mes').each(function(){ if(!$max || $max.data('id') < $(this).data('id')){ $max = $(this); } }); //здСсь Π² $max искомый элСмСнт 

well, or if you do not intend to change html, then

 var $max = null; $('.mes').each(function(){ $(this).data('id', $(this).attr('id').replace('pm','')); if(!$max || $max.data('id') < $(this).data('id')){ $max = $(this); } }); //здСсь Π² $max искомый элСмСнт 
  • This code gives all the contents of id = "pm2515", you only need to know the maximum id)) - skillful
  • and need in jquery - skillful
  • @skillful where issues? what content? This code does not display the contents of the id. The comment is written the same: the variable $ max will contain the required element. - Ivan Pshenitsyn
  • well, the desired element, only it is the minimum, and you need the maximum - skillful
  • @skillful do not understand your problem. You requested an item with a maximum value after pm. after executing the code in $ max, the link is exactly such an element. what's your problem? How do you check work? - Ivan Pshenitsyn