JQuery and attribute problem


I’m quite busy last time, but here is quick post about jQuery bug and workaround.

Following code will populate url with “undefined” instead of correct url.

1
2
var html  = "<a href='http://example.com/content/uploads/2011/05/cat.flv'>cat</a>";
var url = jQuery('a',html).attr('href');

To fix this you should wrap a into p tag. Like this:

1
2
var html  = "<a href='http://example.com/content/uploads/2011/05/cat.flv'>cat</a>";
var url = jQuery('a','<p>'+html+'</p>').attr('href');

In this case things will work fine

Tags: ,