user1679941
user1679941

Reputation:

How can I set an attribute on multiple IDs with jQuery?

I have the following code:

         $('#loginLink, #registerLink')
            .attr('data-disabled') === 'yes'

I am trying to set the data-disabled attribute on the IDs but it does not seem to work. Am I using the correct jQuery?

Upvotes: 5

Views: 4327

Answers (4)

Adriano Carneiro
Adriano Carneiro

Reputation: 58595

You set the value of an attribute like this:

$('#loginLink, #registerLink').attr('data-disabled','yes');

To retrieve a value stored like this, you can either:

$('yourselector').attr('data-disabled');

or

$('yourselector').data('disabled');

Upvotes: 4

NappingRabbit
NappingRabbit

Reputation: 1918

$('#loginLink, #registerLink').attr('data-disabled','yes')

maybe? I am not familiar with that attribute, but this is how they are usually attributed.

Upvotes: 0

Praveen
Praveen

Reputation: 1449

Generally in jQuery, any attribute should be set as below:

$(<selector>).attr("<attr name>", "<attr value>");

Hope this Helps!!

Upvotes: 1

Ram
Ram

Reputation: 144669

You should set the value this way:

$('#loginLink, #registerLink').attr('data-disabled', 'yes');

As data is a property, you can use data method instead:

$('#loginLink, #registerLink').data('disabled', 'yes');

Upvotes: 11

Related Questions