Utilize JavaScript in a Rails 4.2.1 application to implement functionality for toggling the display of the search bar

Could you please help me figure out how to delay the JavaScript hide function until after my searches have been completed? This way, I can continue making further searches without any interruptions.

Every time I try to make a search and press enter, my search bar suddenly disappears from view.

To better understand the issue, take a look at the screenshot below;

https://i.sstatic.net/Q2Ig1.png

index.html.erb

<a href="#" class="toggle-formed" style="float: right;" >Search</a>

                <div id="sample" class="<%= @xvaziris_data.present? ? 'hidden' : '' %>">

                    <%= form_tag xvaziris_path, method: 'get', class: "form-group", role: "search" do %>
                    <p>
                        <center><%= text_field_tag :search, params[:search], placeholder: "Search for.....", class: "form-control-search" %>
                            <%= submit_tag "Search", name: nil, class: "btn btn-md btn-primary" %></center>
                        </p>
                        <% end %><br>

                        <% if @xvaziris.empty? %>

                        <center><p><em>No results found for.</em></p></center>              

                        <% end %>

                    </div>

search.js

$(document).on('page:change', function () {
        $("div#sample").hide();

    //    | === HERE
    $("a.toggle-formed").click(function(event) {
        event.preventDefault();
        $("div#sample").fadeToggle();
    });
});

general.scss

.hidden {
  display: none;
}

I would appreciate any suggestions or feedback on this matter.

Thank you in advance.

Answer №1

My issue was resolved by incorporating an Ajax search functionality that dynamically updates data without needing to refresh the page or hide the search bar during searches. The search bar only disappears when the page is refreshed or reloaded. Problem solved!

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

Switch the following line utilizing a regular expression

Currently, I am facing a challenge with a large file that needs translation for the WordPress LocoTranslate plugin. Specifically, I need to translate the content within the msgstr quotes based on the content in the msgid quotes. An example of this is: #: . ...

Cannot find WoopraTracker within the custom event data

I am currently working on implementing Woopra custom event data upon page load by following their guidelines. I have attempted to push events when the page is ready, however, it keeps returning an error that woopratracker is not defined. Strangely, when ...

What are the steps for combining two constants with the JSON format of "const = [ { } ]"?

Here is the initial code that I would like to merge with another section. const sections = [ { title: section1_title, rows: [{ title: section1_option01, rowId: "sec1_option01", ...

Guide on exporting type definitions and utilizing them with npm link for a local package

I am in the process of developing a new testing tool called tepper as an alternative to supertest. My goal is to make this package available in both ESM and CJS formats. However, I'm encountering an issue where users of the library are unable to locat ...

The OnsenUi getCurrentPage() function returns an empty object

As I work on creating a hybrid mobile app using Onsen UI, I've encountered an issue regarding navigation and data transfer between two pages. My starting point is index.html, which consists of a main.html ons-template along with ons-navigation and ons ...

Comparing object variables within Javascript's HTMLCollection

let var1, var2; // Obtaining elements from the HTMLCollection var1 = document.forms[0]; var2 = document.forms.item(0); alert(var1 === var2); // Output: "true" var1 = document.forms["myForm"]; var2 = document.forms.namedItem("myForm"); alert(var1 === v ...

Troubles arise when attempting to utilize yarn for my projects

Whenever I enter the command yarn start in the terminal, I get this output: yarn run v1.22.4 $ react-scripts start 'react-scripts' is not recognized as an internal or external command, operable program or batch file. error Command fai ...

Interact with a modal element using puppeteer

I'm having trouble clicking on the email login button inside a modal using Puppeteer for automation. The code is not able to find the modal element. Can someone assist me in debugging this issue? const puppeteer = require('puppeteer'); ( ...

When you run `npm install`, it removes the React Router from the package-lock

I recently encountered an issue where, after deleting node_modules from my app and running npm install, the package-lock.json file was altered and react-router was removed from it. This resulted in errors stating that the module react-router could not be f ...

What is the best way to transform a class containing a map to and from a JSON string in JavaScript?

Recently encountered a challenge at work. Here is an example of a class I have: class A { map1: Map<number, number> = new Map(); map2: Map<string, string> = new Map(); // a bunch of other data age: number = 0; ... } The goa ...

Is there a way to implement a setTimeout delay in my puppeteer browser?

I wrote a function named waitForTimeout to introduce a brief delay that can be awaited once the browser navigates to a new page. function waitForTimeout(timeout) { return new Promise((resolve) => { setTimeout(resolve, timeout) }) } const puppet = () =& ...

Tips for establishing a connection with an MQTT mosquito broker in a React application

I am currently working on establishing a connection between my React web application and a mosquito broker that is hosted on Docker. I have decided to utilize the MQTT.js library for this purpose, which you can find here. Below is the code snippet that I ...

JavaScript has encountered an Uncaught TypeError due to an illegal invocation

I am currently developing a lambda function that calls another function with specific parameters. While this code runs without issues in Firefox, it encounters an error in Chrome, displaying a strange message in the inspector: Uncaught TypeError: Illegal ...

Ways to duplicate the content of a div to the clipboard without the use of flash

Simple question! I have a div identified as #toCopy, along with a button identified as #copy. Can you suggest the most efficient method to copy the content of #toCopy to the clipboard upon clicking #copy? ...

Tips for deleting an item within a nested array object in JavaScript

Could you please advise on how to remove an object based on a condition using Javascript? If the trans cost is > 200, then keep it in obj, otherwise remove it var obj1 = [{ "id": "trans", "option": "bank", "cost": "100" }, { "id": "fund", "o ...

Creating a factory class in Typescript that incorporates advanced logic

I have come across an issue with my TypeScript class that inherits another one. I am trying to create a factory class that can generate objects of either type based on simple logic, but it seems to be malfunctioning. Here is the basic Customer class: cla ...

Exclusive gathering to discuss @Input attributes

Is there a way to determine if all of the input properties in my component have been initialized with data? Are there any specific events that can help indicate this? Thank you for your assistance! ...

Create a new array containing two objects that have values taken from two separate objects with the same key in Javascript

I've been attempting to achieve something I've been wanting to do: I have two objects with the same keyName but different values. I need to create a new array that contains a new object with two entries, each holding the values from the two obje ...

Dynamic Expandable Element

I'm currently working in JavaScript without utilizing any external libraries like jQuery or CSS animations. Within my project, I have a Div element with an unspecified height that adjusts itself based on the content it holds. The content within this ...

Is it possible to extract a user's password from the WordPress database for use in a React application

Is it feasible to create a React application integrated with Postgres within Wordpress that operates on MySql? I am interested in leveraging Wordpress's built-in features for theming, posts, and user management accounts. However, I am unsure if I can ...