Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmsdsmd sm,dfsadf
1,124 PointsI am trying to change the attribute false to true when i clicked on it, but it isn't working.
<!DOCTYPE html>
<html>
<body>
<div aria-checked="false" style="height:30px;width:30px;border:2px solid #000;border-radius:50%;box-sizing:border-box;">
<script>
const div = document.querySelector('div')
div.addEventListener('click',function(){
if(div.getAttribute('aria-checked')){
div.setAttribute('aria-checked','false')
}
else{div.setAttribute('aria-checked','true')}
})
</script>
</body>
</html>
It works fine when I clicked it once, but when I double-clicked, it won't change its attribute.
2 Answers
Rich Donnellan
Treehouse Moderator 27,696 PointsTechnically, the value of div.getAttribute('aria-checked')
returns a string ("false"
/"true"
) because that is the only value allowed in an HTML attribute. You'll either need to check if this value is a string (div.getAttribute('aria-checked' === 'true'
), or you can convert to a Boolean (JSON.parse(div.getAttribute('aria-checked')
).
Personally, I would write it like so:
const div = document.querySelector('div');
let isChecked = JSON.parse(div.getAttribute('aria-checked')); // Converts initial value to Boolean
div.addEventListener('click', function () {
this.setAttribute('aria-checked', !isChecked); // "this" being the clicked element and since we're changing its attribute
isChecked = !isChecked; // Flips the state to the opposite of what it was. Used above too.
});
msdsmd sm,dfsadf
1,124 Points<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div style="height:30px;width:30px;border:2px solid red;" role="checkbox" aria-checked="false" tabindex="0">
</div>
<script>
const div = document.querySelector('div')
div.addEventListener('click',function(){
switch(div.getAttribute('aria-checked')){
case 'true': //Can you explain to me what this means case = 'true'
div.setAttribute('aria-checked','false')
break;
case 'false': //Can you explain to me what this means case = 'false'
div.setAttribute('aria-checked','true')
break;
}
})
</script>
</body>
</html>
Rich Donnellan
Treehouse Moderator 27,696 PointsWhy did you refactor to use switch/case
? Your if
check would have done the job (see end of comment).
Per MDN:
A case clause used to match against expression. If the expression matches the specified valueN, the statements inside the case clause are executed until either the end of the switch statement or a break.
As I mentioned above, the actual value of the attribute is a string ("false"/"true"
). The case
explicitly checks for the value "true"
(string).
Refactoring your original code:
const div = document.querySelector('div');
div.addEventListener('click', function () {
if (div.getAttribute('aria-checked') === 'true') {
div.setAttribute('aria-checked', 'false');
} else {
div.setAttribute('aria-checked', 'true');
}
});
Rich Donnellan
Treehouse Moderator 27,696 PointsRich Donnellan
Treehouse Moderator 27,696 PointsQuestion updated with code formatting. Check out the Markdown Cheatsheet below the Add an Answer submission for syntax examples, or choose Edit Question from the three dots next to Add Comment to see how I improved the readability.