Skip to content

Commit 729d9c9

Browse files
committed
setImage helper function for course card
1 parent e633d0c commit 729d9c9

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

web-components/course-card/course-card.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ function createComponent(html) {
3636
link.setAttribute('aria-label', `Learn more about ${name}`);
3737
}
3838

39+
/**
40+
* Sets the content of an element based on a CSS selector.
41+
* @param {string} cssSelector - The CSS selector of the element to set the content for.
42+
* @param {string} src - The image source to set for the element.
43+
* @param {string} name - The name of the element containing the image.
44+
* @param {boolean} shadow - A boolean indicating whether to use the shadow DOM for the element.
45+
* @returns {void}
46+
*/
47+
function setImage(cssSelector, src, name, shadow) {
48+
const courseImg = shadow.querySelector(cssSelector);
49+
courseImg.src = src;
50+
courseImg.setAttribute('alt', `${name} badge`);
51+
}
52+
3953
// Web component class representing a course card.
4054
class CourseCard extends HTMLElement {
4155

@@ -70,10 +84,7 @@ function createComponent(html) {
7084
shadow.innerHTML = html;
7185

7286
// Set course img
73-
const courseImg = shadow.querySelector('.course-img');
74-
courseImg.src = this.imgsrc;
75-
courseImg.setAttribute('alt', `${this.name} badge`);
76-
87+
setImage('.course-img', this.imgsrc, this.name, shadow);
7788
// Set course name
7889
setContent('.course-name', this.name, shadow);
7990
// Set course desc

0 commit comments

Comments
 (0)