Reputation: 11
<li class="catalog-list-item" data-icon="false">
<a href="/items/170893265">
How would I use console.log to log the href /items/IDHERE
(IDHERE
= 170893265) by using catalog-list-item?
$.get("http://m.roblox.com/Catalog/CatalogSearch?startRow=0&keyword=&filter=Collectibles",
function(Data) {
var test = $(Data).find('.catalog-list-item')
var itemID = test.attr("href");
console.log(itemID);
});
And that won't work for me (the test part does work, not the itemID part though.)
Upvotes: 0
Views: 42
Reputation: 24638
You left out the a
tag, which is the one you're targeting -- the one that has the href attribute you're looking for:
var test = $(Data).find('a'); //<<<<-------
var itemID = test.attr("href");
console.log(itemID);
To get just the id
use:
var itemID = test.attr("href").split('/').pop();
Upvotes: 1
Reputation: 816302
If
<li class="catalog-list-item" data-icon="false">
<a href="/items/170893265">
is the full response, then li.catalog-list-item
will be the selected element in $(Data)
. I.e. $(Data).find('.catalog-list-item')
would try to find a .catalog-list-item
element inside a .catalog-list-item
element, which doesn't work.
You can .filter
the selection and then search for the a
element(s):
var test = $(Data).filter('.catalog-list-item').find('a');
Upvotes: 1
Reputation: 467
you realize the href
attribute isn't in the li
tag. I suggest you do this
var test = $(Data).find('.catalog-list-item')
var itemID = test.find('a').attr("href")
console.log(itemID)
Upvotes: 0