Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Displaying filtered content

Hey guys,

I'm trying to create a phonebook that displays pictures for each selection of the entry. I took this code from W3Schools and essentially, what I want it do is to display each of the person's picture once I've filtered for them. E.g. if I filter for "Agnes", I want the page display a picture of "Agnes" and not just the name that I filtered. Unfortunately, I don't know how to go about this. Any help is appreciated.

Code:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
</style>
</head>
<body>

<h2>My Phonebook</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<ul id="myUL">
  <li><a href="#">Adele</a></li>
  <li><a href="#">Agnes</a></li>

  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>

  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
</ul>

<script>
function myFunction() {
    var input, filter, ul, li, a, i, txtValue;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        txtValue = a.textContent || a.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";
        }
    }
}
</script>

</body>
</html>
 
Hey,

I actually tried that, however that doesn't really serve the purpose I'm trying to achieve as I would need to click on the name "Adele" for the picture to be displayed via a link. My ideal function would for the picture to be displayed upon applying the filter.

Thanks for the help anyway :)
 
This works for me:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
</style>
</head>
<body>

<h2>My Phonebook</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<ul id="myUL">
  <li><a href="#">Adele <br> <img width=128px src="https://image.shutterstock.com/image-vector/african-woman-national-headdress-portrait-600w-1332000272.jpg"/></a></li>
  <li><a href="#">Agnes <br> <img  width=128px src="https://image.shutterstock.com/image-vector/long-hair-girl-avatar-flat-600w-1294453510.jpg"/></a></li>

  <li><a href="#">Billy <br> <img width=128px src="https://image.shutterstock.com/image-vector/short-hair-sunglasses-boy-avatar-600w-1294453507.jpg"/></a></li>
  <li><a href="#">Bob <br> <img width=128px src="https://image.shutterstock.com/image-vector/curly-hair-boy-avatar-flat-600w-1294453498.jpg"/></a></li>

  <li><a href="#">Calvin <br> <img width=128px src="https://image.shutterstock.com/image-vector/man-face-emotive-icon-smiling-600w-551814742.jpg"/></a></li>
  <li><a href="#">Christina <br> <img width=128px src="https://image.shutterstock.com/image-vector/portrait-asian-girl-pink-hair-600w-1397661953.jpg"/></a></li>
  <li><a href="#">Cindy <br> <img width=128px src="https://image.shutterstock.com/image-vector/long-hair-sunglasses-girl-avatar-600w-1294453561.jpg"/></a></li>
</ul>

<script>
function myFunction() {
    var input, filter, ul, li, a, i, txtValue;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        txtValue = a.textContent || a.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";
        }
    }
}
</script>

</body>
</html>
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom