5

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?

2

4 Answers 4

11

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');
Sign up to request clarification or add additional context in comments.

2 Comments

On using data, beware that it would not be available as an attribute anymore
@Adrian Yes, the value is stored as a property. As we use prop for selected or checked properties we should use data for data-* attributes.
4

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');

Comments

1

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

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

Hope this Helps!!

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.