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?

  • 1
    The question has been answered in another question: http://stackoverflow.com/questions/4604099/handling-multiple-ids-in-jquery – SamHuckaby Oct 30 '12 at 17:42
  • try as given in this post, using each function http://stackoverflow.com/questions/7079011/jquery-multiple-id-selectors – Alagesan Palani Oct 30 '12 at 17:44

4 Answers4

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');
Ram
  • 143,282
  • 16
  • 168
  • 197
  • On using data, beware that it would not be available as an attribute anymore – Adriano Carneiro Oct 30 '12 at 17:44
  • @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. – Ram Oct 30 '12 at 17:46
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');
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
1

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

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

Hope this Helps!!

Praveen
  • 1,449
  • 16
  • 25
0

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

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

NappingRabbit
  • 1,888
  • 1
  • 13
  • 18