icecub
icecub

Reputation: 8773

Javascript / jQuery: Why do I keep getting undefined?

I have the following html code:

<a class="tabTitle" id="title_boxtab_default" tab-type="default" href="#tabs-3">Sample</a>

In jQuery I'm trying to get the value of the href attribute with:

$('.tabTitle').last().attr('href');

But it keeps telling me it's undefined?

I should mention that the number of links with class"tabTitle" increases. That's why I use .last()

Upvotes: 0

Views: 84

Answers (3)

Sridhar R
Sridhar R

Reputation: 20418

Try DOM Ready

$(document).ready(function(){
$('.tabTitle').last().attr('href');
});

Fiddle

Upvotes: 0

David Hellsing
David Hellsing

Reputation: 108500

It should work. Make sure you are running the script when the element is present in the DOM, f.ex using .ready():

$(function() {
    alert( $('.tabTitle').last().attr('href') );
});

DEMO: http://jsbin.com/IyeSacU/1/edit

Upvotes: 0

Mike Thomsen
Mike Thomsen

Reputation: 37506

Works just fine for me in JSFiddle. I changed it to this, and it returned #tabs-3:

alert($('.tabTitle').last().attr('href'));

My guess is that it's not running from a ready handler like this:

$(function() {
    var x = $('.tabTitle').last().attr('href');
});

Upvotes: 1

Related Questions