69

I have an element on my page, where i only will know part of its id, e.g. _moComments_:

id="MainContent_listSelectedDays_listDayBanks_0_moComments_0"

How can i find an element by partial id (e.g. using jQuery)?

For example (jsFiddle):

<input type="text" id="MainContent_listSelectedDays_listDayBanks_0_moComments_0">

with script

$('[id=_moComments_]').val("Found it");

Bonus Reading

Community
  • 1
  • 1
Ian Boyd
  • 233,966
  • 238
  • 834
  • 1,160

1 Answers1

134

You could use the "Attribute Contains Selector":

$('[id*="_moComments_"]')

However you'd probably be better off by simply adding a class or a custom [data-*] attribute and selecting based on that.

zzzzBov
  • 167,057
  • 51
  • 314
  • 358