d
Amit DhamuSoftware Engineer
 

ID Is Like

1 minute read 00000 views

You may have a series of divs on your page which are named similarly such as div1, div2, div3 etc. Without declaring all of those in a jQuery selector, you can filter them as shown below.

HTML

<div id="box1"></div>
<div id="box2"></div>
<div id="box3"></div>

jQuery

// Starts with
$('div[id^=box]').on('click', function () {
  // Do stuff here
})

// Ends with
$('div[id$=box]').on('click', function () {
  // Do stuff here
})

// Contains with
$('div[id*=box]').on('click', function () {
  // Do stuff here
})