In just a few words, my question is what are the limitations for a string to be a searchable id or searchable content in jQuery.
Update: I got the ID part, but not why I can't even search in the html content with that string. A bouns for anyone willing to tell me a regex to change the pattern from "MM/dd/yy HH:mm:ss" to "MMddyyHHmmss"
Details follow:
I'm trying to obtain the text inside multiple <p>
s using jQuery.
The way I'm adding them is with the following code:
$("<p>"+data.timeStamp + " _ " + data.text +"</p>")
.addClass(data.source)
.attr("id", "msg_"+data.userId+"_"+data.timeStamp)
.appendTo($("#messages_"+data.userId));
They look fine and I can add as many as I can. But then, I need to obtain the text inside each one in order to append a response. I use the userId
and the timeStamp
to find which paragraph I need to append to. It works just fine if timeStamp
is just a number (e.g., created by the java servlet as
SimpleDateFormat df = new SimpleDateFormat("MMddyyHHmmss");
but jQuery is unable to find it if the timeStamp is a little more complicated:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
or even
SimpleDateFormat df = new SimpleDateFormat("MM-dd-yy HH.mm.ss");
I tried bo开发者_高级运维th
var txt = $("#msg_"+data.message.from+"_"+data.message.timeStamp).html();
and
var txt = $("#messages_"+data.userId).find(":contains('"+data.message.timeStamp+"')").html();
to no avail.
The JSON is encoded and decoded fine during the ajax call, since console.log (response.timeStamp);
will give the right output.
Any ideas? Should I escape somehow the timeStamp
string to use it in the jQuery search?
PS 1: If you find any syntax error, it is most probably due to the cut & paste and simplification of the code for the example, since with a simple timeStamp I can make it work in the real js file.
PS 2: I know that a workaround in my specific case would be to use two different strings, one for the time stamp in the text and one (simpler) for the id, but that does not help me understand the limitation here.
You need to have a valid value for your element IDs. From the html 4 spec:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
But you will propably have problems with jQuery css selectors when using colons and periods in your IDs, since these are parsed by jQuery as class names and pseudo-elements.
So your element IDs should only contain letters, digits, hyphens and underscores.
Based on the comments and the answer, I tried to escape with a double backslash the string, but it didn't work (still no results), even when searching the text and not using those long IDs. So, I ended up removing spaces and symbols on the id part by doing on the client:
var ts = data.timeStamp.replace(/[\:\/ ]/g, "");
and on the server:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
Then I added the formatted date to the text and the stripped down to the ID. It works, though I am still curious about what the limitations are for jQuery search strings.
精彩评论