-1

I am using jQuery to locate an element in my web page, but it always throws an error at this line:

var test = $('table[id$=' + array[i].Id + ']');

The error is Object doesn't support this property or method.

array[i].Id is "12312 Tab1"

When I remove the space in the Id, there is no error thrown. My jQuery knowledge is very basic, so I am not quite sure why this error would occur?

Any ideas please?

coffeeak
  • 2,980
  • 7
  • 44
  • 87
  • ====> http://stackoverflow.com/questions/596314/jquery-ids-with-spaces might help `:)` fiddle it and make a demo. – Tats_innit Aug 21 '14 at 03:36
  • try using backslash "12312\ Tab1" – kenicky Aug 21 '14 at 03:36
  • I always recommend to [read the **documentation**](http://api.jquery.com/attribute-ends-with-selector/) first (emphasis mine): *"`jQuery( "[attribute$='value']" )` [...] value: An attribute value. Can be either an **unquoted single word** or a **quoted string**."* – Felix Kling Aug 21 '14 at 03:40
  • generally classes can be multiple separated by space but for id there is only one so spacing in id wouldn't be recommended to use... – Bhojendra Rauniyar Aug 21 '14 at 03:44

2 Answers2

1

Use this:

var test = $('table[id$="' + array[i].Id + '"]');

you need add quotes if the attr value contains space

dencey
  • 1,041
  • 17
  • 25
0

You need to either escape the character occurence or wrap whole value in quotes when using in attribute value selector:

var test = $('table[id$="' + array[i].Id + '"]');

or

var test = $('table[id$=' + array[i].Id.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")+ ']');
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125