3

I am upcoming learner with jquery. Does anyone know how to give set two attributes at once that fit into the below example?

For the moment I am only able to give it one attribute. My goal is to able to give two attributes to a select box 'disabled' and 'hidden'

obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true); 

Same things goes for removing the attributes: 'disabled' and 'hidden'

obj.next('.update').html(data.list).removeAttr('disabled');

2 Answers2

4
obj.nextAll('.update')
 .attr({
    disabled: true,
    hidden: true
 })
 .html('<option value="">----</option>');

According to @nnnnnn comment .prop() might be more appropriate than .attr()

i.e

obj.nextAll('.update')
 .prop({
    disabled: true,
    hidden: true
 })
 .html('<option value="">----</option>');

DEMO for adding multiple attr

To remove

// from jQuery1.7, it can be a space-separated list of attributes.

obj.next('.update').html(data.list).removeAttr('disabled hidden');

DEMO for remove multiple attr

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • 1
    is there such thing hidden attribute? Is it HTML5? – Naor May 29 '12 at 07:14
  • Note that [`.prop()`](http://api.jquery.com/prop/) might be more appropriate than `.attr()` (assuming jQuery version >= 1.6). @Naor - Yes, html5. http://stackoverflow.com/questions/6708247/what-is-the-difference-between-the-hidden-attribute-html5-and-the-displaynone – nnnnnn May 29 '12 at 07:22
  • @thecodeparadox Hey my friend! once again Thank you. I have learned alot from you today and i appreciate the demos :). I was setting those values incorrectly and now i see why it wasn't working. –  May 29 '12 at 07:23
  • @thecodeparadox sorry I forgot check your answer. But yes the issues have been resolved. –  May 29 '12 at 08:19
0

You can do something like this:

obj.attr({ firstAttr:firstVal,  secondAttr:secondVal});
Naor
  • 23,465
  • 48
  • 152
  • 268