-2

I have a site which has an element with a specific id. I would like to get the href attribute using jQuery. I am trying to do the following:

alert($("#id").prop('href'));

But it says 'undefined'. I tried setting the timer function as well as using jQuery's delay(), but the result is the same, although I can see that the element is there when the alert pops up. What is the problem?

dsgriffin
  • 66,495
  • 17
  • 137
  • 137

1 Answers1

1

Well, you can assure that JS works after HTML is loaded by wraping it into document ready. I've never used prop, but attr is awesome to get attributes. The example I provided should work.

http://jsfiddle.net/mJzr8/

$().ready(function() {
    alert($("#id").attr("href"));
});
Umut Benzer
  • 3,476
  • 4
  • 36
  • 53
  • Yes, I forgot to mention that this was the first thing I tried. The problem is that the element is not in the initial HTML body but it is (probably) dynamically created by another script. It is my understanding that .prop() is supposed to replace .attr() which is deprecated in the newest version of jQuery (it still works though, they both yield the same output). – Mihaly Balas Apr 10 '13 at 21:58
  • Are you sure that newly created element is in the same page but not in an iframe? – Umut Benzer Apr 11 '13 at 06:46