Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(a,this).attr('href') returns undefined

Im using ajax to load some data from a mysql database... my problem is, it getting the id for the data i want to load, i have set the HREF value as the id... so an example is:

`<a href="16" title="View Story: ssasds">ssasds</a>`,

16 is the id value i need... my code is:

$('.artefact').click(function()
                {

                var storyId = $('a',this).attr('href');
                console.log(storyId);
                               }

when i check the console (firebug) it just says undefined. please try and help out, as i have tried other methods of getting the data but gets messy.

thanks

like image 898
nmyster Avatar asked Apr 11 '11 10:04

nmyster


2 Answers

Seems like .artefact has two as. Based on this:

$('.artefact').click(function () {
    var storyId = $('a', this).filter("[href]").attr('href');
    console.log(storyId);
});

EDIT

On second thought, this looks cleaner:

$('.artefact').click(function () {
    var storyId = $(this).find("a[href]").attr('href');
    console.log(storyId);
});
like image 119
Salman A Avatar answered Sep 29 '22 20:09

Salman A


Is .artefact a link? If yes, why to use $('a', this) instead of just $(this)?

like image 27
Olegas Avatar answered Sep 29 '22 18:09

Olegas