Reputation: 7543
I happened to have super dumb issue and I'm stuck.
I do console.log(data) and I get exactly this:
<a href='http://www.someurl.com'>caption</a>
The question is how do I get this links "href" attribute.
I have absolutely no idea why, but these doesn't work:
data.text()
== Uncaught TypeError: undefined is not a function (should be: caption)
$('a',data).attr('href')
== undefined (should be: http://www.someurl.com)
Maybe this is not a string, but object or something else? How to check that? My JS looks like this:
window.send_to_editor = function(data) {
var videourl = data;
console.log(videourl.text()); // Uncaught TypeError: undefined is not a function
console.log(videourl); // <a href='http://www.someurl.com'>caption</a>
}
Upvotes: 0
Views: 338
Reputation: 29221
In your case, data is a string, not a jQuery object and therefore does not have of jQuery's methods (like text
).
If you are certain that data
is a string containing a link, you can use a regular expression to extract the link like so:
var match = data.match(/href='(.*)'/g);
url = match && match[1];
console.log(url);
Alternately, you can create a jQuery object from your string. But that's a much more expensive operation if you just want to get the url.
Upvotes: 0
Reputation: 14649
It's not a jQuery object. That is why it is undefined. First, create a jQuery object.
var _videourl = $(videourl);
console.log(_videourl.text());
console.log(_videourl.attr('href'));
// caption (index)
// http://www.someurl.com
Upvotes: 0
Reputation: 1841
You should first find out, what type data
is. To do this you can use the JavaScript builtin function typeof(data)
.
Upvotes: 1
Reputation: 9597
Using jQuery, you can do something like that:
var data = "<a href='http://www.someurl.com'>caption</a>";
var link = $(data).attr('href');
It will create dynamically your DOM element, then you will be able to get your attribute href.
Upvotes: 1