1

I'm making a custom sorting tool for my custom gridview.

I use jQuery ui $.autocomplete() to find DISTINCT VALUEs from a COLUMN in a TABLE.

I then add the selected values to a jquery ui $.selectable().

I usually use the id attribute to store distinct type values like this (such as TABLE PRIMARYies), so my problem comes in when I try to store these values where they probably will not be compliant with HTML id standards or readable by jQuery.

How can I create a function to escape and de-escape any text from a database into HTML id readable by jQuery and back again?

  • 1
    Why store these as an id? Why not use `.data()` where you can store any string or javascript data type with no restrictions? – jfriend00 Apr 19 '13 at 15:58

2 Answers2

2

To do the encoding/decoding, you can use the functions described in this answer

To do a jquery lookup you can use $('[id="' + escapedId + '"]');. It's not the most efficient, but it will guarantee that none of your special characters will be mistaken for part of the selector.

Community
  • 1
  • 1
cfs
  • 10,610
  • 3
  • 30
  • 43
1

You can store custom attributes on any HTML object with the data- prefix as per the HTML5 spec (which is backwardly compatible with earlier versions of HTML too). These can be used in selector queries (the same way any attribute is used in a query) or can be retrieved with jQuery's .data(). There are still a few restrictions on the characters that can be in a custom attribute like quoting, but it is less restrictive than an id value.

jfriend00
  • 683,504
  • 96
  • 985
  • 979