Jquery Javascript get elements ids starting with and ending with a specific word or string

by Rahul Gupta

HTML

<p id="item5_2"> A -> id="item5_2"</p>
<p id="item9_5"> B -> id="item9_5"</p>
<p id="item14_2"> C -> id="item14_2"</p>
<input type="button" value="Get element ids starting with 'item' and ending with '2'" onclick="get_ids()">

JavaScript

get_ids = function(){
    $('[id^="item"][id$="2"]').each(function() {
        alert($(this).attr('id'));
    });
}