71

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
  • 246,734
  • 253
  • 869
  • 1,219

1 Answers1

139

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
  • 174,988
  • 54
  • 320
  • 367