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,379
  • 4
  • 17
  • 29

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
  • 10,487
  • 2
  • 16
  • 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
  • 35,563
  • 9
  • 45
  • 62
0

Ue attr

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

or prop

$('a[href="http://www.google.com"]').prop('id');
coolguy
  • 7,728
  • 9
  • 43
  • 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
  • 166,373
  • 34
  • 252
  • 304