I search this site a lot and have had many good answers, this is the first time I've needed to ask a question because I just can't find an answer.
My script is injected into pages that users load, so those pages could be totally arbitrary structure. There's always some special text on the page that I want to be able to retrieve automatically the next time they visit that page (it's a live value so I'm kind of scraping this value).
The pages are so arbitrary, the only way I can do it is to have the user double click on the value to 'train' the script and detect the event and the element that was double clicked. This works fine, I can retrieve the value I want and I have a jQuery object for the element.
What I can't figure out is how to store that element (or more precisely a selector for it) so I can retrieve the data next time they go to the page without needing them to double-click it.
Here's a jsFiddle http://jsfiddle.net/troywray/YvSH9/ and if you double-click the red text, the text I want is easily retrieved. But what can I store away to be able to reproduce the selector later?
$('body').bind("dblclick", function(event){
var dblClickElement = $(event.target);
var specialText = dblClickElement.html();
alert("I picked up: " + specialText);
console.log(dblClickElement);
console.log("selector result:", $(dblClickElement).selector); // empty string
});
Console says that dblClickElement is an object [span.abcde] but it's a clickable object in firebug. What I want is the string 'span.abcde' so I could use that to select in future. There doesn't seem to be a property of the jquery object that equals span.abcde
I thought I'd found something relevant here: Get the current jQuery selector string? but it doesn't work here (proof is in the fiddle and I get why).
I don't really want suggestions for parsing inner/outer html to build a selector string - if I have to do that, then no doubt I can figure that out.
Also, I do realise that in many cases the selector might not give me a single unique element but I can handle that.