Browse Source

Exercise 5: adding a class without using addClass().

Frederic G. MARAND 8 years ago
parent
commit
1520b85982
1 changed files with 12 additions and 0 deletions
  1. 12 0
      Chapter 5/05.js

+ 12 - 0
Chapter 5/05.js

@@ -72,4 +72,16 @@ $(document).ready(function () {
       $this.wrapInner('<b/>');
     }
   });
+
+  $('p').each(function () {
+    var $this = $(this);
+    var sClass = $this.attr('class') ? $this.attr('class') : '';
+    var aClass = sClass.split(' ');
+    // In a more general case, we would also need to :
+    // - remove multiple spacers
+    // - order and deduplicate existing classes.
+    aClass.push('inhabitants');
+    sClass = aClass.join(' ').trim();
+    $this.attr('class', sClass);
+  });
 });