-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmutationobserver-attributes-filtered.html
57 lines (49 loc) · 1.91 KB
/
mutationobserver-attributes-filtered.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<title>Observing changes to attributes</title>
<link rel="stylesheet" href="mutationobserver.css">
</head>
<body>
<header>
<h1>Observing changes to attributes</h1>
<nav><a href="http://dev.opera.com/articles/view/mutation-observers-tutorial/">Return to article</a></nav>
</header>
<article>
<p>In this example, clicking the <b>Toggle class / data</b> button will toggle a 'modded' class on our <code>div</code> element, and switch the <code>data-config</code> attribute between two values. <em>However</em>, in this case, we've added an <code>attributeFilter</code> to only observe the <code>class</code> attribute. Notice that we don't have a mutation record for the <code>data-config</code> attribute.</p>
<p>Open the developer console to examine changes between states.</p>
<p>
<button type="button">Toggle class / data</button>
</p>
</article>
<div id="demo" data-config="Demonstration">
<p>Lorem ipsum dolar sit amet consecutum.</p>
<p>Lorem ipsum dolar sit amet consecutum.</p>
<p>Lorem ipsum dolar sit amet consecutum.</p>
<p>Lorem ipsum dolar sit amet consecutum.</p>
<p>Lorem ipsum dolar sit amet consecutum.</p>
</div>
<script>
(function(d){
var btn = d.querySelector('button'),
clickhandler = function(){
var demo = d.getElementById('demo'),
olddata = 'Demonstration',
newdata = "Observing multiple attribute changes.";
demo.classList.toggle('modded');
( demo.dataset.config == olddata ) ? demo.dataset.config = newdata : demo.dataset.config = olddata;
}
btn.addEventListener('click',clickhandler,false);
var mocallback = function(mutationrecords){
mutationrecords.map( function(mr){
console.log( mr );
});
}
mo = new MutationObserver(mocallback),
options = {'attributes':true,'attributeOldValue':true,'attributeFilter':['class']},
mo.observe( d.getElementById('demo'),options);
})(document);
</script>
</body>
</html>