JSFiddle - React, Tailwind, and code Playground
HTML
<span id="id1">1</span>
<span id="id2">2</span>
<span id="id3">3</span>
<span id="id5">5</span>
<span id="id4">4</span>
JavaScript
// create an array
var ar = new Array();
// for each element with id that starts with 'id'
$('[id^="id"]').each(
function(){
// add it to the array (only its numeric part)
ar.push(
// extract the numeric part to be added in the array
parseInt( $(this).attr('id').replace('id','') )
);
});
// find the max value in the array
alert('id' + Math.max.apply( Math, ar ));