I have an application that saves a clicked on jQuery selector for later use and I am running into a funny issue when I try and recall the selector. Below is my code for reference. This is a callback function for an AJAX call that returns a JSON object saved in data. I have verified that the data is getting transfered correctly but it seems like trying to use the string as a selector in jQuery again doesn’t work. The String() stuff is just for a test and I am not sure it is necessary.
If anyone has any guidance on why this might not be working I would love to know. Any code with jQuery(selector) just does not function.
jQuery.each(data.dot_collection, function() {
x_coord = this.x_coord;
y_coord = this.y_coord;
selector = new String(this.selector);
//Works
alert( selector ); // = html > body.home.blog.logged-in.single-author.two-column.right-sidebar > div.hfeed > header#branding > hgroup > h1#site-title
//Works
alert(jQuery("html > body.home.blog.logged-in.single-author.two-column.right-sidebar > div.hfeed > header#branding > hgroup > h1#site-title").offset().left);
// Not Working & jQuery(selector).offset() = 'undefined'
alert( jQuery(selector).offset().left );
I assume the problem is that you are passing a
Stringobject instead of a primitive string and jQuery cannot detect this.Try: