What causes the ul element to display upon clicking the input field?

Why is the ul disappearing with display: none when the input element is clicked? Here is the HTML code snippet: When this input is clicked:

 <input class="input country" type="text"/>

This unordered list should be displayed (but it's not):

 <ul id='country-list'>
                <li id="US">United States</li>
 </ul>

Check out the jQuery code below:

function handleCountries() {
    $('#country-list').hide();
    $('#country').click(function () {
        $("#country-list").show();
    });
};

function handleTestOut() {
    handleCountries()
};

$(handleTestOut)

HTML structure to showcase the issue:

 <div class="container">
            <input class="input country" type="text"/>
            <ul id='country-list'>
                <li id="US">United States</li>
            </ul>
            <button>explore</button>
        </div>

Here are some CSS styles being applied:

.container {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 200px;
}

.input {
  height: 30px;
  margin-bottom: 50px;
}

ul {
  height: 200px;
  width: 18%;
  overflow: hidden;
  overflow-y: scroll;
  position: absolute;
  background-color: blue;
  top: 20px
}

Access the complete code here

Note: Invalid HTML has been removed, but the issue persists

Answer №1

If you want to conceal it, just use this code: $('#country-list').hide();

Why are these functions calling each other?

It seems odd that you're using jQuery to call the handleTestOut function, which then calls the handleCountries function. It doesn't really serve a purpose.

You can achieve the same outcome by simply removing them from the functions:

Your main issue was that you were referencing an id as #country when it should be a CSS class called .country for click events (or use input.country or .input class, or combine them all as input.country.input):

$('#country-list').hide();

$('.country').click(function() {
  $("#country-list").show();
});
html {
  height: 100%;
}

body {
  font-family: 'Nunito', sans-serif;
  color: #1b4639;
  background: #cbe2e4;
  height: 100%;
}

.wrapper {
  height: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

p {
  text-align: center;
  margin: 0;
  font-size: 24px;
}

.container {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 200px;
}

.input {
  height: 30px;
  margin-bottom: 50px;
}

ul {
  height: 200px;
  width: 18%;
  overflow: hidden;
  overflow-y: scroll;
  position: absolute;
  background-color: blue;
  top: 20px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" />
  <link href='http://fonts.googleapis.com/css?family=Nunito:400,300' rel='stylesheet' type='text/css'>
  <title>Document</title>
</head>

<body>
  <div class="wrapper">
    <p>Hello World!</p>
    <div class="container">
      <input class="input country" type="text" />
      <ul id='country-list'>
        <li id="US">United States</li>
        <li id="AF">Afghanistan</li>
        <li id="AX">Åland Islands</li>
        <li id="AL">Albania</li>
        <li id="DZ">Algeria</li>
        <li id="AS">American Samoa</li>
        <li id="AD">Andorra</li>
        <li id="AO">Angola</li>
        <li id="AI">Anguilla</li>
        <li id="AQ">Antarctica</li>
        <li id="AG">Antigua and Barbuda</li>
        <li id="AR">Argentina</li>
        <li id="AM">Armenia</li>
        <li id="AW">Aruba</li>
        <li id="AU">Australia</li>
        <li id="AT">Austria</li>
        <li id="AZ">Azerbaijan</li>
        <li id="BS">Bahamas</li>
        <li id="BH">Bahrain</li>
        <li id="BD">Bangladesh</li>
        <li id="BB">Barbados</li>
        <li id="BY">Belarus</li>
        <li id="BE">Belgium</li>
        <li id="BZ">Belize</li>
        <li id="BJ">Benin</li>
        <li id="BM">Bermuda</li>
        <li id="BT">Bhutan</li>
        <li id="BO">Bolivia, Plurinational State of</li>
        <li id="BQ">Bonaire, Sint Eustatius and Saba</li>
        <li id="BA">Bosnia and Herzegovina</li>
      </ul>
      <button>explore</button>
    </div>
  </div>
</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can someone show me how to populate select options with values from a list using Python variables?

Instead of hardcoding values in my HTML file, I have a variable called 'myVariable' in my Python file that is a list. I want to use this variable to generate the options in my select menu. <select class="form-select" aria-label=" ...

Dynamic menu plugin for JQuery

I am still new to jQuery and currently learning how to efficiently use it in website development. I am currently working on implementing a responsive navigation plugin that I came across online. However, I have encountered an issue with the plugin where th ...

Refreshing information within a table using Ajax Prototype

For my current project, I am utilizing PrototypeJS. I have implemented Ajax.PeriodicalUpdater to insert real-time data as required. However, I am facing an issue where the data is not getting replaced inside a specific table. Below is the HTML code snippet ...

Issue with initial width calculation of Slick slider

Having issues with implementing the Slick slider. The calculated width of each slide seems to be incorrect due to padding on the right side of the image. <div class="responsive-slick"> <img src="Gallery/large/01.jpeg"> <img src="Gal ...

show a picture and text side by side

Here's the CSS code I'm using: <style type="text/css"> #box { width:100%; height:80px; background-color:#eeeeee; } .box-inner { width:80%; margin:0 auto 0 auto; text-align:center; } #text, #text a { font-siz ...

The jQuery animate() method fails to execute animations

I'm currently working on a JavaScript animation project and I've run into some roadblocks. One particular issue I'm facing is with animating the <label>'s margin-top, as it's not behaving as expected. For example: $(' ...

Ways to retrieve the content and characteristics of a span element

I have this code snippet : <br> var value = $("#editor").jqxEditor('val'); value="<div><span style="font-weight:bold">Welcome</span></div>"; Now I want to extract the text inside the span tag and its font-weight a ...

Having trouble with changing the class using Jquery click function

Originally, I had the code in an ASP.Net project, but to troubleshoot, I moved it to a separate web server. However, I am still unable to switch between the tab panes with the buttons. Any assistance in debugging this issue would be greatly appreciated. S ...

Position an element in the middle of the range between the tallest and shortest characters

Is there a way to vertically center an element between the tallest and shortest characters of another element (preferably using just CSS, but JavaScript is also acceptable)? I want it to be aligned with the actual text content rather than the line height, ...

Instructions on how to assign a class number to a div matching the cookie number

Cookie: $(document).ready(function(){ //hide all divs for the purpose of this example, you should have them hidden with your CSS //$('.picture.1').hide(); //check if the cookie is already set if ($.cookie("currentDiv") === ...

Utilize jQuery $.ajax to integrate a RESTful WCF service within an MVC 4 web application

I'm currently working on an MVC 4 application and am attempting to consume a RESTful WCF service using the jQuery $.ajax function, but I'm encountering some issues. Here is the jQuery code I have written: $(document).ready(function () { $.ajax( ...

Unusual Ajax response detected by JSF ajax listener

Inside one div, there is a form containing two input text fields and a button. Upon clicking the button, the method createProject.register is triggered and the values from the input fields are saved in a database table. <h:outputText id="opt" value="# ...

The Eonasdan DateTimePicker plugin for Bootstrap v4 (Tempus Dominus) is experiencing technical difficulties

When it comes to my Javascript file, I am including and using it as shown below: import 'moment'; import 'tempusdominus-bootstrap-4'; export default class AddHolidayPane { static Initialise() { $('#start-date').d ...

The usage of event.returnValue has been phased out. It is recommended to utilize the standard event.preventDefault()

Here's the code snippet I'm working with: <script> $(document).ready(function () { $("#changeResumeStatus").click(function () { $.get("{% url 'main:changeResumeStatus' %}", function (data) { if (data[&apos ...

What is the best way to activate a click event when I set a radio button to checked?

I am facing an issue with an uninitialized property in my app.component.ts: color!:string; I am trying to automatically initialize the color property when a radio button is selected: <div> <input type="radio" name="colors" ( ...

Transferring JavaScript to a different page using EJS

Feeling a little stuck here and could use some assistance. It seems like a simple issue, but I'm tired and could really use a fresh pair of eyes to take a look. I'm trying to pass the 'username' variable, but since I'm new to this ...

When the user presses either the refresh button or the back button, they will be redirected

After the user presses refresh or uses the browser back button, I need to redirect them to a specific page in order to restart the application. My JavaScript code is as follows: var workIsDone = false; window.onbeforeunload = confirmBrowseAway; function ...

Troubleshooting a Windows Phone HTML5 app encountering an Ajax error

Currently, I am working on developing a Windows Phone application specifically focusing on creating a "Windows Phone HTML 5 app". However, I have encountered an issue while using ajax with Jquery (version 1.9.1). The snippet of my code is as follows: ...

Issues with DnD functionality in Chrome, images not visible during dragging operation

At first, I encountered an issue with event.dataTransfer.setDragImage not functioning properly in Chrome. This can be seen in the example below: http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=html5-59.htm Upon further investigation, I noticed th ...

Using jQuery and Ajax to send the content of a single textbox to a controller for processing

I am currently developing a timesheet application and I want to incorporate an autosave feature. Here is an example of the form structure: <form id="timesheet" action="save_timesheet.html" method="POST"> <input type="text" id="day1taskid2" /> ...