Issue
I have a problem with my code. I have two anchors that show different content on the page when clicked. I also made them focus when clicked. But I don’t know how to make the current active anchor focused when the page loads for the first time.
I looked online for a solution, but none of them worked, so I’m hoping to get some help here!
Here is my code:
HTML
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div class="insandstu">
<h1 class="insandstuh1">More than just books.</h1>
<a class="insanchor" href="javascript:void(0)" onclick="showInstructors();">FOR INSTRUCTORS</a> <span> | </span>
<a class="stuanchor" href="javascript:void(0)" onclick="showStudents();">FOR STUDENTS</a><br>
<!-- FOR INSTRUCTORS -->
<div id="ins-container">
<p class="forins" id="forins1">LMS Integration</p>
<p class="forins" id="forins2">Test banks</p>
<p class="forins" id="forins3">Answer guides</p>
<p class="forins" id="forins4">Powerpoint slides</p>
<a class="forinsanchor" id="forinsanchor" href="">Explore now <img src="./greater-than.svg" alt="greater-than sign" height="12px" style="color: #0dc0dc;"></a>
</div><br>
<!-- FOR STUDENTS -->
<div id="stu-container">
<p class="forstu" id="forstu1">Highlighting</p>
<p class="forstu" id="forstu2">Note-taking</p>
<p class="forstu" id="forstu3">Multiple book formats</p>
<p class="forstu" id="forstu4">Free online</p>
<a class="forstuanchor" id="forstuanchor" href="">Explore now <img src="./greater-than.svg" alt="greater-than sign" height="12px" style="color: #0dc0dc;"></a>
</div>
</div>
</body>
</html>
Css
.hidden {
display: none;
}
.insandstu {
background-color: white;
padding: 40px 80px;
margin-bottom: 20px;
}
.insandstuh1 {
font-size: 30px;
}
.insanchor {
text-decoration: none;
color: #4c7db8;
cursor: pointer;
}
.insanchor:focus {
color: black;
border-bottom: 2px solid black;
}
.stuanchor {
text-decoration: none;
color: #4c7db8;
cursor: pointer;
}
.stuanchor:focus {
color: black;
border-bottom: 2px solid black;
}
/* For Instructor part */
#ins-container {
padding-top: 26px;
}
.forins {
padding: 20px;
font-weight: bold;
display: inline-block;
}
#forins1 {
border-left: 4px solid #0dc0dc;
border-radius: 2px;
width: 120px;
}
#forins2 {
border-left: 4px solid #fdbd3e;
border-radius: 2px;
width: 90px;
}
#forins3 {
border-left: 4px solid #0c9372;
border-radius: 2px;
width: 100px;
}
#forins4 {
border-left: 4px solid #c22032;
border-radius: 2px;
width: 130px;
}
#forinsanchor {
border-left: 4px solid transparent;
border-image: linear-gradient(0deg, #F36B32, #002469, #C02336, #FABC4D, #25C0DA) 4;
border-radius: 2px;
font-weight: bold;
box-shadow: 0 0.2rem 0.4rem rgba(0, 0, 0, 0.2);
padding: 25px 50px 20px 20px;
text-decoration: none;
color: #026aa1;
}
/* For Student part */
#stu-container {
display: none;
}
.forstu {
padding: 20px;
font-weight: bold;
display: inline-block;
}
#forstu1 {
border-left: 4px solid #0dc0dc;
border-radius: 2px;
width: 120px;
}
#forstu2 {
border-left: 4px solid #fdbd3e;
border-radius: 2px;
width: 90px;
}
#forstu3 {
border-left: 4px solid #0c9372;
border-radius: 2px;
width: 100px;
padding: 10px 20px;
position: relative;
top: 7.3px;
}
#forstu4 {
border-left: 4px solid #c22032;
border-radius: 2px;
width: 130px;
}
#forstuanchor {
border-left: 4px solid transparent;
border-image: linear-gradient(0deg, #F36B32, #002469, #C02336, #FABC4D, #25C0DA) 4;
border-radius: 2px;
font-weight: bold;
box-shadow: 0 0.2rem 0.4rem rgba(0, 0, 0, 0.2);
padding: 25px 50px 20px 20px;
text-decoration: none;
color: #026aa1;
}
JavaScript
function showInstructors() {
// Get the rows by their id
let instructorsRow = document.getElementById("ins-container");
let studentsRow = document.getElementById("stu-container");
// Remove the hidden class from the instructors row
instructorsRow.classList.remove("hidden");
studentsRow.style.display = "none";
// Add the hidden class to the students row
studentsRow.classList.add("hidden");
}
function showStudents() {
// Get the rows by their id
let instructorsRow = document.getElementById("ins-container");
let studentsRow = document.getElementById("stu-container");
// Remove the hidden class from the students row
studentsRow.classList.remove("hidden");
studentsRow.style.display = "block";
// Add the hidden class to the instructors row
instructorsRow.classList.add("hidden");
}
Solution
If you want to focus elements, you can use the focus
method in your JavaScript functions: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus .
But you should also note that might have some accessibility concerns in your code:
- You should prefer using buttons, not anchors, for this use-case. Anchors should be for links.
- It looks like what you're really building here is tabs. In that case, consider having a look at https://daily-dev-tips.com/posts/a11y-101-tabs
Answered By - Willow
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.