tags:

views:

7661

answers:

3

I have this in my HTML

<a class="deleteLink" href="system/id">link</a>

Then using jQuery, I run this in $(document.ready(function()

alert($("a.deleteLink").attr("href"));

Which shows system/id

But running this:

alert($("a.deleteLink").href);

Shows undefined

If I add an Id to the a tag like this

<a id="myId" class="deleteLink" href="system/id">link</a>

Then

alert(myId.href);

shows http://localhost/system/id which is what I want to get from jQuery (so I can do something like

$.post($("a.deleteLink").href);

Can anyone help me with getting the full href value out of jQuery (1.2.6) please?

A: 

try this :

alert(("#myId").attr("href"));

or if you want to use the class:

alert(("a.deleteLink").attr("href"));

*edit on 31/08/2010: added () for the alert...

Kennethvr
You just show a alert box with "myId" or "a.deleteLink".
okoman
this doesnt retrieve any object and causes an error
codeninja
this is not wrong, he just forgot the "()"alert(("a.deleteLink").attr("href")); should work
VP
+1  A: 

Now I see what you want to do.

Try this:

alert( $("a.deleteLink")[0].href );
okoman
+10  A: 

$("...") returns a jQuery object (also called "wrapped set"). The wrapped set does not have a href attribute. If you do this:

alert($("a.deleteLink")[0].href);

It should return the href attribute properly. When you deal with "myId", you're dealing with a DOM element object. Quite different to a wrapped set. The index operator I used there is short for get(0) and that returns an element from the wrapped set.

cletus
Note: the element returned by either indexing objects in the wrapped set directly or using the .get() method will be a pure DOM object, which is why you are able to do "[element].href".
roosteronacid