Can't access object in jquery (prevObject?)
A function in my WP plugin just accidentally (as far as I can tell) stopped working.
Here is the code in question:
window.send_to_editor = function(html) {
var classes = jQuery('img',html).attr('class');
var items = classes.split(" ");
... more stuff here
}
I have verified that the html variable is indeed an img html tag. This is what firebug shows when I do the console.log object ( console.log (jQuery ('img', html)) ):
Object[]
context -> undefined
jquery -> "1.11.2"
length -> 0
prevObject -> Object[img.alignnone.size-full.wp-image-1234 name.jpg]
And the error displayed: classes are undefined .
I believe there is something wrong with the object I am receiving, but this has been used to work lately and I am not aware of any changes on the site that could cause this.
I would be grateful for any data on this matter.
EDIT:
Additional Information. This happens with two plugins that need to be unlinked (made by different people). This happens when, after uploading an image to the server (or selecting a previously uploaded image), you try to insert it into a post.
As I said, this error came out of nowhere, it worked as expected a couple of days ago. The only thing I can think of has changed since then is the domain name, but I don't see how it might be related.
source to share
The selector jQuery
always returns jQuery object
, but if length
- 0
then no elements were found that matched the selector you specified. In your example, you confirmed that nothing was selected because the length
jQuery object is 0
. Check if the item has been selected as follows:
var $els = jQuery('img',html),
classes;
if ($els.length) {
classes = $els.attr("class");
}
Be aware that your DOM request is limited to what you pass as the html parameter. If you just want to find images on a page:var $els = jQuery('img');
source to share