I need to set all of the ‘a’ elements in a ‘div’ element as one class. Is there a way to do this using the ‘div’ element, some other element, or possibly CSS, or no? I am fairly new to HTML.
<ol id='list'><div class="column"><base class="animals">
<li><a href=""><p>yes</p></a></li>
<li><a href=""><p>yeah</p></a></li>
</base>
</div>
</ol>
My first thought was to use the base element but nothing really carried over.
I’m trying to make a search bar mechanism and I need all a elements to be as one class to comply with the JS:
function search_animal() {
let input = document.getElementById('searchbar').value;
input = input.toLowerCase();
let x = document.getElementsByClassName('animals');
for (i = 0; i < x.length; i++) {
if (!x[i].innerHTML.toLowerCase().includes(input)) {
x[i].style.display = "none";
} else {
x[i].style.display = "list-item";
}
}
}
3
Answers
JS Example:
Instead of trying to set the element as one class directly, find the parent element and get what it includes (the initial element you were trying to set as a class), then modify the parent element primarily using JS.
First, OL/UL are allowed LI as children, not base/div etc. And base shouldn’t be used like that too.
I updated your HTML so it is correct, I also removed the p tags as they are meant to be used for paragraph text.
for referencing all of the anchor tags via CSS, you can use #list a as the space automatically references matching "children".
UPDATE
With the new information, I updated my answer to include an improved javascript search function.
You can loop through elements from querySelectorAll and pass selectors without needing a specific class.
I also updated the javascript to toggle an active class to hide/show the results.
To set the class of an
<a>
element, simply addclass="your_class"
to your<a>
element. I would also suggest removing the<p>
element from your<a>
element.Additionally, the
<base>
element is not a correct element. I would change that to a<div>
.