I am attempting to create a search box for a webpage where users can enter a keyword, and my code will search that keyword in the href of all elements. It will then hide any elements that do not contain the keyword in their href.
I have implemented a JavaScript code that reads the text in the href and the search box, and checks if the text in the search box is a substring of the string in the href. I am using the indexOf function in JavaScript to check for substrings.
The expected behavior is that the code should display elements that contain the keyword in their href and hide those that do not. However, currently nothing seems to be happening when the code is executed.
function MyFunction() {
var container, value, txtvalue, i, searchtxt, get;
get = document.getElementById("searchtext");
searchtxt = get.value.toUpperCase();
container = document.getElementById("back");
value = container.getElementByTagName("a");
for (i = 0; i < value.length; i++) {
txtvalue = value[i].href;
if (txtvalue.toUpperCase.indexOf(searchtxt) > -1) {
value.style.display = "";
} else {
a.style.display = "none"
}
}
}
.back {
width: 100%;
height: 625px;
}
.linkbox {
width: 11%;
height: 34%;
background-color: green;
top: 4%;
display: inline-block;
border: 1px solid black;
margin-left: 2.5%;
margin-top: 4%;
left: 3.5%;
box-shadow: 0px 20px 10px -8px black;
border-radius: 12px;
}
.text {
width: 100%;
top: 50%;
position: relative;
text-align: center;
font-size: 100%;
overflow: hidden;
}
<body style="margin:0; background-color:yellow;">
<input type="text" id="searchtext" placeholder="search here" />
<button class="toclick" onclick="MyFunction()"></button>
<div class="back" id="back">
<a href="1-spiderman-far-from-home.html">
<div class="linkbox" style="background:url('images0.jpg'); background-size:100% 100%;">
<div class="text"></div>
</div>
</a>
<a href="2-Aladdin-2019.html">
<div class="linkbox" style="background:url('images1.jpg'); background-size: 100% 100%;">
<div class="text"></div>
</div>
</a>
<a href="3-venom-2018.html">
<div class="linkbox" style="background:url('images2.jpg'); background-size:100% 100%;">
<div class="text"></div>
</div>
</a>
<a href="4-Alita-battle-angel-2019.html">
<div class="linkbox" style="background:url('images3.jpg'); background-size:100% 100%;">
<div class="text"></div>
</div>
</a>
<a href="5-Antman-and-the-wasp.html">
<div class="linkbox" style="background:url('images4.jpg'); background-size:100% 100%;">
<div class="text"></div>
</div>
</a>
<a href="6-Fantastic-beasts-crime-of-grindelwald-2019.html">
<div class="linkbox" style="background:url('images5.jpg'); background-size:100% 100%;">
<div class="text"></div>
</div>
</a>