Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find an element by partial ID in jQuery? [duplicate]

Tags:

jquery

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

  • jQuery selector documentation
  • jQuery select with partial name
  • Making partial matches with jQuery?
like image 650
Ian Boyd Avatar asked Sep 11 '13 18:09

Ian Boyd


1 Answers

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.

like image 107
zzzzBov Avatar answered Oct 22 '22 02:10

zzzzBov