.attr ('class') is undefined in jQuery ()

I have a problem with my jquery function in a wordpress theme. I used this particular function in harsh times, and it worked. The function returns the identifier of the image that was downloaded through the wordpress loader.

Here is the relevant part:

// extract the img-ID from class attribute
var jt = jQuery('img', html).attr('class');
alert(html);
alert(jt);
j1 = jt.split(' ');

Both alerts are only for finding out what is happening. alert (html) returns this:

<img src="http://www.xyz.com/files/2012/03/stage.jpg" alt="" title="" width="1000" height="371" class="alignnone size-full wp-image-6" />

but alert (jt) returns "undefined".

Any idea? It would be great if you could help.

Jan

+5
source share
8 answers

context html . jQuery DOM, jQuery. HTML.

DOM, .

DOM html, , DOM.

var html = '<img src="http://www.xyz.com/files/2012/03/stage.jpg" alt="" title="" width="1000" height="371" class="alignnone size-full wp-image-6" />';
var item = jQuery(html);
var jt = item.attr('class');
alert(jt);
j1 = jt.split(' ');

, : http://jsfiddle.net/jfriend00/phnED/

+6

:

jQuery('img', html)

html , -, html - , img img, .

EDIT:
html - <img src="http://www.xyz.com/files/2012/03/stage.jpg" alt="" title="" width="1000" height="371" class="alignnone size-full wp-image-6" />, :

var klass = $(html).attr('class');

// You could do this to create an array of all classes
var klasses = $(html).attr('class').match(/\S+/g);
+2

html, , undefined , DOM, .

$(document).ready( function() {
// extract the img-ID from class attribute
var html = jQuery('.some-class');
var jt = jQuery('img', html).attr('class');
alert(html);
alert(jt);
j1 = jt.split(' ');
});

http://api.jquery.com/jQuery/

http://jsfiddle.net/ssUhJ/

+1

attr('class') . , var (html) - undefined.

, , HTML5 .

0

html- :

 $(html).attr('class');
0

html

var jt = jQuery('img').attr('class');
alert(jt);
j1 = jt.split(' ');
alert(j1);
0

The problem is in var jt = jQuery('img', html), from the jquery document , the second parameter is the context of the jquery object. check out the modified program at http://jsfiddle.net/hYKnd/ .

0
source

As in jquery 1.6 (see docs for .attr ), I .attrswitched from returning an empty string to returning undefined.

Open this jsfiddle http://jsfiddle.net/QVqXC/ and change the downloaded version of jquery and see how the output message changes.

-1
source

All Articles