hiding the search box by clicking away from it

Can someone help me with modifying this search box design?

@import url(http://weloveiconfonts.com/api/?family=entypo);

/* entypo */
[class*="entypo-"]:before {
  font-family: 'entypo', sans-serif;
  color: #C0C0C0;
}

* {
  margin: 0px;
  padding: 0px;
  border: 0px;
  outline: 0px;
  box-shadow: 0px;
}

body {
  background-color: pink;
}

#container {
  height: 300px;
  margin-top: 10%;
}

.entypo-search:before {
  position: absolute;
  top: 0.4em;
  left: 0px;
}

#form {
  background-color: white;
  border-radius: 30px;
  padding: 10px 20px;
  width: 13px;
  overflow: hidden;
  position: absolute;
  height: 30px;
  right: 50%;
  -webkit-transition: width .55s ease;
  -moz-transition: width .55s ease;
  -ms-transition: width .55s ease;
  -o-transition: width .55s ease;
  transition: width .55s ease;
}

form {
  position: relative;
}

input {
  width: 0px;
  height: 25px;
  font-size: 15px;
  margin-left: 30px;  
  /* margin-top: 3px; */
  line-height: 30px;
}

.entypo-search {
  position: absolute;
  top: 20%;
}

#form:hover {
  width: 200px;
}

#form:hover form input {
  width: 200px;
}

#form form input:focus, 
#form form input:active{
  outline:none;
  width: 300px;
}
<div id="container">
    <div id="form">
    <form action="#" class="entypo-search">
      <fieldset><input id="search" placeholder="Search" /></fieldset>
    </form>  
  </div>
</div>

Currently, the search box expands and collapses when hovered over. However, it presents an issue when a user clicks on a suggestion from the dropdown, causing the search box to collapse unexpectedly. I am looking to modify this behavior so that the search box only closes when clicking outside of the box with the mouse.

Appreciate any suggestions or solutions. Thank you.

Answer №1

I have successfully implemented a JavaScript feature that detects when the user hovers over the #form element and clicks on the .container element. I have also made improvements by removing the use of the :hover CSS selector and replaced it with a class that is toggled on and off within the event listeners.

// Function to close the form when clicking its container
document.getElementById('container').addEventListener("click", function() {
  document.getElementById("form").classList.remove("active");
});

// Preventing the form from closing when it is clicked
document.getElementById('form').addEventListener("click", function(event) {
  event.stopPropagation();
});

// Opening the form on mouseover
document.getElementById('form').addEventListener("mouseover", function() {
  document.getElementById("form").classList.add("active");
});
@import url(http://weloveiconfonts.com/api/?family=entypo);

/* Styling for the entypo icon set */

[class*="entypo-"]:before {
  font-family: 'entypo', sans-serif;
  color: #C0C0C0;
}
* {
  margin: 0px;
  padding: 0px;
  border: 0px;
  outline: 0px;
  box-shadow: 0px;
}
body {
  background-color: pink;
}
#container {
  height: 300px;
  margin-top: 10%;
}
.entypo-search:before {
  position: absolute;
  top: 0.4em;
  left: 0px;
}
#form {
  background-color: white;
  border-radius: 30px;
  padding: 10px 20px;
  width: 13px;
  overflow: hidden;
  position: absolute;
  height: 30px;
  right: 50%;
  -webkit-transition: width .55s ease;
  -moz-transition: width .55s ease;
  -ms-transition: width .55s ease;
  -o-transition: width .55s ease;
  transition: width .55s ease;
}
form {
  position: relative;
}
input {
  width: 0px;
  height: 25px;
  font-size: 15px;
  margin-left: 30px;
  /* margin-top: 3px; */
  line-height: 30px;
}
.entypo-search {
  position: absolute;
  top: 20%;
}
#form.active {
  width: 200px;
}
#form.active form input {
  width: 200px;
}
#form form input:focus,
#form form input:active {
  outline: none;
  width: 300px;
}
<div id="container">
  <div id="form">
    <form action="#" class="entypo-search active">
      <fieldset>
        <input id="search" placeholder="Search" />
      </fieldset>
    </form>
  </div>
</div>

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

The qTip comment box vanishes when focused on by IE/Edge browsers using touch devices

A unique JavaScript library called qTip is being utilized in my project. The application includes a StarRating and Comments feature, both of which are enabled by this plugin. After selecting a rating using the stars, users have the option to add comments, ...

Dealing with checked input type='checkbox' in React - A guide

Having a set of checkboxes, some already checked and some to be updated by the user. The issue here is that while the checkboxes render correctly initially, they do not change upon clicking. The 'checked' value does get updated when onChange is t ...

How to save data to a JSON file using the filesystem module in Node.js

After coming across this helpful guide at , I was intrigued by the creation of a point system in discord.js. The use of let points = JSON.parse(fs.readFileSync("./points.json", "utf8")); to read the file piqued my interest, leading me to explore how to bui ...

What is a way to center content vertically without using extra containers?

I've been facing challenges in vertically aligning an item within its container. Despite trying multiple suggestions that advise wrapping the content in another div or container, I encountered various issues which I won't delve into. Therefore, ...

Is it possible to trigger the execution of two functions simultaneously by using onClick in JavaScript?

I currently possess: one = () => { //perform a task } two = () => { //execute an action } <div> <button onClick={/*this.one, this.two (it doesn't function)*/}>Go</button> </div> Is there a way to invoke two f ...

The expected functionality of sending files via ajax is not happening as anticipated

I am having issues with passing file data along with other inputs to my ajax function. Despite my best efforts, the server is not receiving the files. I'm fairly new to using ajax and Jquery. Below is the code snippet of what I have attempted so far. ...

Social media icons condensed into a single line within a compact menu

I have come across a problem that I can't seem to solve. How can I display social icons in one line within a collapsed menu? I added the following code to my CSS file but nothing seems to have changed. Any suggestions or ideas would be greatly appreci ...

How can I remove a specific JSON object from localStorage based on the data associated with the element that is currently being clicked on?

Unique Scenario A user interacts with an element, triggering a change in the background image and storing data related to that element in localStorage: this process functions correctly. Next, the toggle variable is set to 0, the background image change ...

Invoke the componentDidMount() method in a React Component that is not a subclass of React.Component

I have a react component that I render later in my index.js file index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <React.StrictMode> <App /> ...

Unable to view the token balances of the smart contract on remix while executing the seeBalance function

pragma solidity =0.7.6; pragma abicoder v2; import "https://github.com/Uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; interface IERC20 { function balanceOf(address account) external view returns (uint256); function transfer(address ...

Tips for extracting a substring from a string in JavaScript or ReactJS

Is there a way to extract a specific string from a website URL in ReactJS? For example, if the URL is https://www.mrkashyap.com/users/manish, I only want to retrieve the word manish or anything after users/. I currently have the URL stored in a variable ...

Yeoman - A guide for integrating an express server task into Gruntfile.js from the generator-angular template

Currently, I am diving into the world of Grunt and attempting to integrate an Express server into my AngularJS application that was initially created with Yoeman. I've made adjustments to the following task as shown below: grunt.registerTask('s ...

Do I need to recompile anything after making changes to a node module in my vue.js project in order for the modifications to take effect? The updates are not appearing as expected

Currently tackling a Vue.js project and came across an issue with the Bootstrap-vue module that I would like to remove. After locating the section in the module responsible for this behavior, I commented it out. But strangely, the changes are not showing ...

Preserving classes in JQuery after making AJAX requests

Before we proceed, it's important to note that I am unable to modify any of the existing calls, code, or functions. This means I must come up with a workaround solution. So, here's the situation: I have a form containing various fields and a dro ...

Express.js encountered a FetchError due to receiving an invalid JSON response body from https://api.twitter.com

I am currently working on a project that involves getting the tweet id as form input and using the Twitter API to retrieve data about that specific tweet. However, I have encountered an issue where the JSON data is not being returned properly. router.post( ...

Tips for optimizing Slider Code to showcase an expanded selection of slides

I am currently troubleshooting a slider code on my ASP.NET website. After examining the HTML, JS, and CSS from the page theme, I noticed that only two slides are being displayed. However, when attempting to add a new slider HTML and adjusting the CSS for t ...

There is an issue with the project where main.jsbundle, Foundation, and Security are missing, resulting in

I've noticed that I am missing the main.jsbundle file in my project and I'm unsure how to resolve this issue. Should I delete the file, or is there a specific step I can take to fix it? Attached is a screenshot showing where the file should be l ...

Use custom styles or CSS for the file input element: <input type="file">

Can CSS be used to achieve consistent styling for <input type="file"> in all browsers? ...

Python-JavaScript Integration Problem

I'm a beginner when it comes to Javascript, Python, and PHP. In my Javascript program, I have a button that triggers a Python script and displays the returned value on the screen. My goal is to steer clear of using Jquery and Json. Here are the snipp ...

The data from the Rails hidden_field or JavaScript array is being submitted in the form of a comma-separated string instead of

Previously, I utilized a collection_select to create a <select> element with <options>. <%= f.label :members, class: "mdl-textfield__label" %> <%= f.collection_select :user_ids, User.where.not(id: current_user).collect, :id, :emai ...