1
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
    </script>
<a href="http://www.google.com" id="aGoogle1">Google Link</a>
<script type="text/javascript">
$(function()
{

        console.log($('a[href="http://www.google.com"]'));
});
</script>

In chrome->console, I can see $('a[href="http://www.google.com"]') returns the selected element, and I can see it has this property: id: "aGoogle1". so my question is:

How to output the property, e.g. id, I tried $('a[href="http://www.google.com"]'.id), but it does not work?

user2507818
  • 2,719
  • 5
  • 21
  • 30

4 Answers4

1

You can use attr() and prop() to get the attributes of an element. However there are some differences between two. check attr() Vs prop(). You can access the id by

$('a[href="http://www.google.com"]').attr('id');

or

$('a[href="http://www.google.com"]').prop('id');
Community
  • 1
  • 1
Ayyappan Sekar
  • 11,007
  • 2
  • 18
  • 22
0

use attr() to get or set attributes

alert($('a[href="http://www.google.com"]').attr('id')); 

this will get the id of selected elements

bipen
  • 36,319
  • 9
  • 49
  • 62
0

Ue attr

  $('a[href="http://www.google.com"]').attr('id');

or prop

$('a[href="http://www.google.com"]').prop('id');
coolguy
  • 7,866
  • 9
  • 45
  • 71
0
$('a[href="http://www.google.com"]'.id)

This code attempts to access the property id on the string object 'a[href="http://www.google.com"]'; the outcome is undefined. Afterwards you're wrapping that inside a jQuery object. The outcome of that is an empty jQuery set.

You need to always start from here:

$('a[href="http://www.google.com"]')

And then use jQuery functions to do what you need. In your case you wish to access the property of the anchor element, so you use prop():

$('a[href="http://www.google.com"]').prop('id')
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309