Occasionally, Javascript experiences intermittent failures but functions properly after the page is refreshed

I am using JavaScript to show and hide different sections of my page:

function showCustomer(str) {
    // remove white space from the name entered on the search screen
    str = str.replace(" ", "%20");
    var xmlhttp;

    if (str == "") {
        document.getElementById("txtHint").innerHTML = "No records found";
        return;
    }

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET", "get_customer.asp?q=" + str, true);
    xmlhttp.send();
}

function enable_submit() {
    document.getElementById("id_simplesearch_submit").style.visibility = "visible";
    document.getElementById("autocomplete-search").reset();
    document.getElementById("txtHint").style.display = "none";
    document.getElementById("results").style.visibility = "visible";
}
function disable_submit() {
    document.getElementById("id_simplesearch_submit").style.visibility = "hidden";
    document.getElementById("id_simplesearch").reset();
    document.getElementById("txtHint").style.visibility = "visible";
    document.getElementById("results").style.display = "none";
}

I have an input field with

onkeyup="showCustomer(this.value)" 

and element with

onfocus="disable_submit()"

and a form defined as

    <div class="commandspace">
    <form id="id_simplesearch" name="simplesearch" method="post" action="index.asp">
    <div class="simplesearch"><label for="forename">Forename</label><input type="text" id="id_forename" name="forename" onfocus="enable_submit()" /></div>

    <div class="simplesearch"><label for="surname">Surname</label><input type="text" id="id_surname" name="surname" onfocus="enable_submit()" /></div>
    <div class="simplesearch"><label for="unit">Unit/Team</label><input type="text" id="id_unit" name="unit" onfocus="enable_submit()" /></div>

    <div id="simplesearchsubmit">
    <div class="simplesearch">
    <input class="simplesearchsubmit" type="submit" id="id_simplesearch_submit" name="search" value="Search" />

When the page is first loaded and I start typing in the showCustomer search box, the txtHint element displays and fetches data. However, if I click into one of the three simplesearch boxes and then back into the showCustomer search box, the txtHint does not display at all.

Answer №1

The issue lies in this location (refer to comments for the solution):

function show_submit_button() {
    document.getElementById("id_simplesearch_submit").style.visibility = "visible";
    document.getElementById("autocomplete-search").reset();
    document.getElementById("txtHint").style.display = "none"; // <----- This uses display
    document.getElementById("results").style.visibility = "visible";
}
function hide_submit_button() {
    document.getElementById("id_simplesearch_submit").style.visibility = "hidden";
    document.getElementById("id_simplesearch").reset();
    document.getElementById("txtHint").style.visibility = "visible"; // <----- This uses visibility
    document.getElementById("txtHint").style.display = "block"; // <----- This line should resolve your issues.
    document.getElementById("results").style.display = "none";
}   

Therefore, show_submit_button hides the element using display, whereas hide_submit_button attempts to display it using visibility. This inconsistency in styling properties is causing the problem.

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

Guide to creating a hierarchical navigation system with HTML

Is there a way to create a menu tree using just HTML and some scripting, without downloading additional software? I tried searching on Google but all the results require downloads. Can anyone provide guidance or help with this task? Thank you in advance. ...

Show the selection of form in a div using React

I am currently working on developing a user interface that dynamically updates based on the selection made by the client in a dropdown menu. The structure I have set up includes a React Parent component named App that renders two child components called In ...

The Autocomplete feature in Material-UI is not rendering any output

Within the render method of a class, an Autocomplete component is causing nothing to appear as rendered; once removed, everything else renders as expected. export default class Home extends Component { render() { return ( ... ...

Establishing the default value of an input field in Angular by referencing another field

Within an Angular (1.5) setting, I am confronted with a form containing two input sections: ID URL The requirements are as follows: If the ID section remains vacant, the URL field should likewise be left empty. If the URL area is entered manually, it ...

Introducing a fresh addition to the array

I am looking for a way to create a new array without modifying the original one. Currently, there is a function that adds a new object to an existing array, but I need a different method to achieve this. Can someone suggest a solution? const arr = [ ...

Why do I keep getting an ExpressionChangedAfterItHasBeenChecked error after trying to update a random color in an

Is there a way to assign a random color from an array without causing the error message: "ExpressionChangedAfterItHasBeenChecked"? Even though the color of the chip changes quickly before the message appears, it seems like it's working. How can I reso ...

The checkbox's select all functionality is malfunctioning when employed with jquery

When the "select all" checkbox is clicked, it only selects the checkboxes on the particular page, instead of selecting all pages. This functionality is implemented using jQuery. The datatable contains hundreds of pages and data graph plotting is performe ...

Using JavaScript to parse JSON response for sending status data through a POST request

I have successfully implemented a PHP code for file uploads. However, I am facing an issue retrieving the JSON response that indicates the upload status. Upon uploading a file via POST method, the response appears as follows: {"html_content":"<p>Fi ...

Tips for preventing page refresh when submitting a form in Django and retaining the page content

regarding the title above: patientdetails.html <form id="content" action="{% url patientdetails_view pk %}" method="post" > {% csrf_token %}{{form.owner}} ------ <tbody><tr> <td><input type="submit" value="Save" > ------ pa ...

Transforming an array of JavaScript objects into arrays of key-value pairs based on a specific property with ES6 syntax

Consider an array of objects like this: myArray = [ {name: 'First', parent: 1, delta: 2}, {name: 'Second', parent: 1, delta: 1}, {name: 'Third', parent: 2, delta: 1} ]; The goal is to transform this array into an objec ...

Updating a div using Ajax within a Rails application

In my to-do app, each task has subtasks stored in div elements with p id="task_"+<%=task.id%>I have a form for creating a new subtask on every show page of my project. To understand better, here is the code I am using: This is the show.html.erb fil ...

Automatically scrolling through a nested list in ReactJs

I have a chat list integrated into my web application. The entire webpage is scrollable, as shown in the image at the bottom. I would like the messages list to automatically scroll to the bottom whenever a new message arrives. To achieve this, I created a ...

Tips for stopping a long poll Ajax request when leaving a page

Is anyone else experiencing issues with long poll Ajax requests not terminating in the browser when leaving a page, especially on Internet Explorer? Despite attempts to abort or stop the connection using standard methods like htmlxml, the request remains ...

AngularJS Currency Converter - Converting Currencies with Ease

I have a question regarding the most efficient way to handle currency conversion on a webpage. Currently, I have multiple input fields displaying different currencies. When a user clicks on the currency conversion button, a modal popup appears. After the ...

A guide to displaying a countdown timer in an Angular application during the app's loading process

Displaying a loader that shows the time in seconds while loading an app is my goal. Here is the code snippet: HTML <body> <div class="app-loader"> <div class="loader-spinner"> <div class="loading-text"></div> ...

How should .has(), .get() and .set() be properly implemented for a JavaScript Map() within an ExpressJS server?

Feeling thrown off by javascript Map()? My query revolves around javascript Map() - the set, get, and has() functions. Despite thinking I was an expert, it seems I still have things to learn... Situation: I'm dealing with a 'global' map sto ...

Click on the lines to highlight them with your mouse or use the arrow keys

Previously, I inquired about how to Highlight lines of text on mouseover. However, after using it for some time, I discovered a few drawbacks. One issue was that users would often lose track of their place when switching tabs or moving the mouse to perform ...

How come I am receiving the E11000 error from Mongo when I have not designated any field as unique?

Encountering an issue while attempting to save the second document to MongoDB Atlas. The error message reads as follows: Error:MongoError: E11000 duplicate key error collection: test.orders index: orderId_1 dup key: { orderId: null } Despite having no un ...

Calculate the sum of multiple user-selected items in an array to display the total (using Angular)

Within my project, specifically in summary.component.ts, I have two arrays that are interdependent: state: State[] city: City[] selection: number[] = number The state.ts class looks like this: id: number name: string And the city.ts class is defined as f ...

Incorporating DefinitelyTyped files into an Angular 2 project: A step-by-step guide

I am currently developing an application using angular 2 and node.js. My current task involves installing typings for the project. In the past, when starting the server and activating the TypeScript compiler, I would encounter a log with various errors rel ...