Skip to content

Commit c896479

Browse files
committed
add dataset notes & update tooltip implementation
1 parent 90269aa commit c896479

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

Advanced-DOM-APIs/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Back to DOM & More Browser APIs (Advanced DOM Interactions - Beyond Querying and Inserting)
2+
3+
| Contents |
4+
| :--- |
5+
| [Using "dataset" (data-* Attribute)](#using-dataset-data--attribute) |
6+
7+
Below topics will be covered and for its practical application, [Planner Project](project_planner/) will be used as reference where concept of classes and functions are demonstrated:
8+
9+
- Attaching Data to Elements
10+
- Working with Element Coordinates and Sizes
11+
- Working with Templates & Dynamic Scripts
12+
- navigator, location and window.history
13+
14+
## Using "dataset" (data-* Attribute)
15+
16+
The `data-` attribute is a way to store custom data attributes on HTML elements, and it can be used to attach additional data to an HTML element without using a non-standard attribute or extending the HTML specification.
17+
18+
JavaScript can access these attributes using the `dataset` property of the element. The `dataset` property is an object that contains all the `data-*` attributes of the element, where each attribute is converted into a camelCase property name.
19+
20+
For example, consider the following HTML element:
21+
22+
```HTML
23+
<div id="example" data-name="John" data-age="30"></div>
24+
```
25+
26+
To access the `data-name` attribute of this element using JavaScript, you can use the `dataset` property like this:
27+
28+
```javascript
29+
const element = document.getElementById('example');
30+
const name = element.dataset.name;
31+
console.log(name); // Output: "John"
32+
```
33+
34+
You can also set the value of a `data-*` attribute using the `dataset` property. For example:
35+
36+
```javascript
37+
element.dataset.name = 'Jane';
38+
```
39+
40+
This will update the `data-name` attribute of the element to "Jane".

Practice-OOPs_Classes_and_DOM/project_planner/assets/scripts/app.js renamed to Advanced-DOM-APIs/project_planner/assets/scripts/app.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ class Component {
3535
}
3636

3737
class Tooltip extends Component {
38-
constructor(closeNotifierFunction) {
38+
constructor(closeNotifierFunction, toolTipText) {
3939
super();
4040
this.closeNotifier = closeNotifierFunction;
41+
this.toolTipText = toolTipText;
4142
this.create();
4243
}
4344

@@ -49,7 +50,7 @@ class Tooltip extends Component {
4950
create() {
5051
const toolTipElement = document.createElement('div');
5152
toolTipElement.className = 'card';
52-
toolTipElement.textContent = "Dummmy";
53+
toolTipElement.textContent = this.toolTipText;
5354
toolTipElement.addEventListener('click', this.closeToolTip);
5455
this.element = toolTipElement;
5556
}
@@ -69,17 +70,19 @@ class ProjectItem {
6970
if (this.hasActiveTooltip) {
7071
return;
7172
}
73+
const projectElement = document.getElementById(this.id);
74+
const toolTipText = projectElement.dataset.extraInfo;
7275
const tooltip = new Tooltip(() => {
7376
this.hasActiveTooltip = false;
74-
});
77+
}, toolTipText);
7578
tooltip.attach();
7679
this.hasActiveTooltip = true;
7780
}
7881

7982
connectMoreInfoButton(){
8083
const prjItemElement = document.getElementById(this.id);
8184
const moreInfoBtn = prjItemElement.querySelector('button:first-of-type');
82-
moreInfoBtn.addEventListener('click', this.showMoreInfoHandler);
85+
moreInfoBtn.addEventListener('click', this.showMoreInfoHandler.bind(this));
8386
}
8487

8588
connectSwitchButton(type) {

Practice-OOPs_Classes_and_DOM/project_planner/assets/styles/app.css renamed to Advanced-DOM-APIs/project_planner/assets/styles/app.css

File renamed without changes.
File renamed without changes.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- [More on Objects](More-on-Objects/README.md)
1212
- [Classes and Object Oriented Programming (OOP)](Classes-and-Object-Oriented-Programming/README.md)
1313
- [Constructor Functions and Prototypes](Constructor-Functions-and-Prototypes/README.md)
14-
- [Practice - OOPs, Classes and DOM](Practice-OOPs_Classes_and_DOM/)
14+
- [Advanced DOM APIs](Advanced-DOM-APIs/README.md)
1515

1616
## References
1717

0 commit comments

Comments
 (0)