Mark said:
Thanks for getting back OT.
The list contains an item such as "'O'Neill & Son'" (In order
to appear correctly in the rendered dropdown).
When selected and placed in the text box, it should appear as "O'Neill
& Son', however it doesn't all the ugly encoding is displayed to the
user.
Seriously, that should not break like that. Are you sure that it does
that? I've not looked into the source code (beyond the first few lines).
If so, I am sorry you got sucked into that one.
Hacking it to decode makes jquery think the input is invalid, as the
selection must appear in the original list...
The way I see it there are 3 options:
1: Hack the jquery library to make the comparison callbacks to
decode / encode between the 2 formats on the fly. (Not sure if it's
possible to do reliably and with tidy code. The whole point of using a
library was to avoid this kind of minefield.)
2: Hack the jquery library to use 2 lists, "HTML" and "TEXT"
formatted. (Same problems as option 1)
3: Ditch the library and do it manually... Hence this post. Any
examples out there of this problem being solved right? It seems like a
common problem, yet the only examples out there use half baked
libraries that only kind of work some of the time.
You'll need to write some code, somewhere along the way. Sounds like the
jQuery widget needs to be redesigned in a way to allow for "O'Neill &"
to be an option to the user, and represented correctly in HTML.
The other posters were on the right track with the textContent/innerText
issue.
Does the value that is displayed to the user the same value that you
want to send to the server?
for example, you might show "O'Neill & Son" (as O'Neill & Son)
to the user, but send a value to the server that is associated with that
value (e.g. "1"). That way, the server side program does not have to
deal with the value of "O'Neill & Son", but can have the value it wants
for that.
Does the list need to be populated with data from the server
/dynamically/, as the user types?
The representation of the list of labelText/value could be done with a
DL, where the DT is displayed and the DL holds the value:
<DT id="autocomplete-items">
<DT>O'Neill & Son</DT>
<DD>x1</DD>
</DT>
or could use an HTML5 datalist element.
<datalist id="autocomplete-items">
<option value="x1">O'Neill & Son</option>
</datalist>
When the INPUT's value changes, then update the displayed options.