33

I want to find all input boxes with id containing _date

My code is like this:

<input type="text" name="creation_date" id="id_creation_date" />

Can I use regex for that? Do I need to install something extra for regex to work with jQuery?

Keavon
  • 6,837
  • 9
  • 51
  • 79
Mirage
  • 30,868
  • 62
  • 166
  • 261
  • I don think you can directly do that way. What I think you can do is . get all the input using $('input') and then filtering out using a regex and $(this).attr('id') – Gaurav Shah Jun 27 '11 at 05:37
  • possible duplicate of [JQuery selector regular expressions](http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions) – Mat Jun 27 '11 at 05:40
  • 2
    Its better to have this link [http://api.jquery.com/category/selectors/](http://api.jquery.com/category/selectors/) – Bharat Patil Jun 27 '11 at 05:47

2 Answers2

59

I'm thinking of one of these...

$('input[id*="_date"]').css('background-color', 'red');

jerluc
  • 4,186
  • 2
  • 25
  • 44
  • thanks buddy that is working , but will it work on all browsers like ie7 – Mirage Jun 27 '11 at 05:46
  • 1
    Yes it will, as jquery is taking care of cross browser compatibility. – Bharat Patil Jun 27 '11 at 05:47
  • It should, as far as I know, even though it uses CSS3-like selectors, it breaks down to this line as I see it in the jQuery source code: `f==="*="?e.indexOf(g)>=0:f==="~=`... meaning it's literally doing a simple (cross-browser) indexOf() check on the specified attribute. – jerluc Jun 27 '11 at 05:49
  • This is beautiful :) – VoA Mar 07 '17 at 20:00
22

If you just want to check ending with "_date" rather than containing "_date" you can use

$('input[id$="_date"]')
Bharat Patil
  • 1,398
  • 1
  • 11
  • 28