2

I need to print some text if the result has similar text, is there a like statement in jquery? Similar to how you use LIKE in SQL?

if (r.events[i].room = 'craig')
                $('#'+r.events[i].slot).append('<br />').append('craighouse');
user3080562
  • 103
  • 1
  • 10

2 Answers2

5

There is no LIKE statement. However, you can search for one string within another using indexOf():

if (r.events[i].room.indexOf('craig') != -1) // -1 = not found
    $('#'+r.events[i].slot).append('<br />').append('craighouse');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

You can use indexOf

It returns -1 if no match is found.

if (r.events[i].room.toUpperCase().indexOf('craig'.toUpperCase()) != -1)
                $('#'+r.events[i].slot).append('<br />').append('craighouse');

also, you can use toUpperCase if you don't want a match that depends on the case of the string.

Rui
  • 4,847
  • 3
  • 29
  • 35