workwise
workwise

Reputation: 1103

Jquery - cannot fetch attribute

Simple line of code:

var page=jQuery("#buy-items-button").attr('id');

Problem is that page is undefined. Relevant portion of html

<li><a id='#buy-items-button' href="#buy-items" data-page="buy-items" class="section-button">Buy Items</a></li>

console.log(jQuery("#buy-items-button")) shows:

[context: document, selector: "#buy-items-button", jquery: "1.9.1", constructor: function, init: function…]
context: document
selector: "#buy-items-button"
__proto__: Object[0]

Driving me crazy as having been using jQuery for long and never faced issues on such trivial calls.

Upvotes: 1

Views: 52

Answers (4)

Arthur.A
Arthur.A

Reputation: 1

The id attribute in your HTML is wrong. Remove the pound sign.

Upvotes: 0

Jai
Jai

Reputation: 74738

You can try with this:

jQuery('a[id^="#buy-items"]').attr('id');

Upvotes: 0

Snake Eyes
Snake Eyes

Reputation: 16764

Your HTML is wrong (remove # from id attribute):

<a id='#buy-items-button' href="#buy-items" 

should be

<a id='buy-items-button' href="#buy-items" 

Upvotes: 0

Felix
Felix

Reputation: 38102

You need to remove # from id value of your anchor:

<li><a id='buy-items-button' href="#buy-items" data-page="buy-items" class="section-button">Buy Items</a></li>

Upvotes: 4

Related Questions