Unveiling the search bar as it expands horizontally from the center, causing the submit button to slide to adjust

Wishes and Aspirations

  • My goal is to design an .input-group with one input[type=text] and one button[type=button].
  • Initially, the text box should be hidden, leaving only the button visible at the center of the page.
  • Upon activation, the text box should expand in width in both directions (left and right), while the button slides to the right, creating an animation that seems to be anchored to the center of the .input-group.

@jakob found this fiddle as a functional example. Using that fiddle and similar ones as references, I have created the following:

Implemented using HTML, CSS, JS, JQuery, and Bootstrap4.

$('document').ready(function() {

  $('#search-label').on({
    mouseenter: function() {
      $('#search-box').stop().animate({
        width: '500px',
        borderBottom: '3px solid #373A3C'
      });
    },
    mouseleave: function() {
      $('#search-box').stop().animate({
        width: '0px',
        border: 'none'
      });
    },
    click: function() {
      $('#search-box').focus();
    }
  });

  $('#search-box').on({
    focusin: function() {
      $(this).css({
        width: '540px',
        borderBottom: '3px solid #373ABC'
      });
    },
    focusout: function() {
      $('#search-box').stop().animate({
        width: '0px',
        border: 'none'
      });
    }
  });
});
// CSS code goes here
<!DOCTYPE html>
<html lang="en">

<head>
  // Head content goes here
</head>

<body>
  // Body content goes here
</body>

</html>

$('document').ready(function() {
  var $label = $('#search-label');
  var $searchBox = $('#search-box');

  function revealSearchBox() {
    $('#search-box').stop().animate({
      width: '500px',
      borderWidth: '3px',
      borderStyle: 'solid',
      borderColor: '#373A3C'
    }, 400);
  }

  function hideSearchBox() {
    $('#search-box').stop().animate({
      width: '0px',
      border: 'none'
    }, 400);
  }

  function focusOnSearch() {
    $('#search-box').focus().toggleClass('focused');
  }


  $label.addEventListener('mouseenter', revealSearchBox());
  $label.addEventListener('mouseleave', hideSearchBox());
  $label.addEventListener('click', focusOnSearch());
});
// CSS code goes here
<!DOCTYPE html>
<html lang="en>

<head>
  // Head content goes here
</head>

<body>
  // Body content goes here
</body>

</html>

I have provided the complete code in case any issues arise from my Bootstrap implementation. However, the only relevant elements are #search-bar, #search-box, #search-submit, and #search-label.

If there are any suggestions or corrections to improve the code, I'm open to feedback. Additionally, if there is a better approach than the one I have implemented, I am keen to learn.

Answer №1

Here is an example of how you can achieve this functionality:

$('document').ready(function() {
  $('#search-bar-label').on('click', function(e) {
     e.preventDefault();
     $('#search-input').focus();
     return false;
  });
  $('#search-bar-label').mouseover(function() {
    $('#search-input').stop().animate({
      width: '500px',
      borderWidth: '1px',
      borderStyle: 'solid',
      borderColor: '#ddd'
    }, 'fast');
  });
  $('#search-bar-label').mouseout(function() {
    if(!$('#search-input').is(":focus")){
      $('#search-input').stop().animate({
        width: '0px',
        borderWidth: '0',
      }, 'fast');
    }
  });
});

By implementing this code snippet, you will achieve the following functionalities:

  • Input field shown/hidden on hover
  • Input field focused on link click
  • Input field remains visible when focused

You can view the full example here.

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

How do I view a wildcard in my ID selector using jQuery?

When I want to initially hide various content scattered around the page, I usually use the following jQuery code: $('#objective_details, #time_estimate_details, #team_members_details, #resources_details').hide(); Is there a method to utilize a ...

Adapting a CSS design

Having some trouble with creating a CSS style. Here's what I have so far: http://jsfiddle.net/WhNRK/26/ Originally designed for a label, but now attempting to modify it for something like this: <input type='radio' name='mcq' ...

Please request user input in order to generate a multiplication chart

Is there a way to ensure that the program works properly so that when the user inputs a value, it is included in the multiplication table? <html> <head> <title> Multiplication Table </title> <style> body{ font-family: aria ...

Tips for turning off the auto save password option on browsers for user registration forms

Is there a way to prevent browsers from saving passwords on the add users form? I've tried using autocomplete="off" but it doesn't seem to work for saved passwords. I've searched extensively for a solution but haven't found the right on ...

What is the best way to iterate through an ID using jQuery?

After pulling the list of doctors in my area from the database and displaying it on my webpage, I now want to load each doctor's "About" content inside a Bootstrap modal. I added an "about" column within the same database table where the doctors' ...

Guide on updating the form structure with ajax

Lately, I've been working on a contact module with 3 columns: name, email, and phone. There's also a +1 button that adds a new row to input another contact using ajax. However, a problem arises when updating the structure as the data in the old c ...

The chosen date is not preserved within the select box

I have these specific items: $scope.items = [{"date":"2014-12-26T05:00:00.000Z","display":"6:00"}, {"date":"2014-12-26T05:15:00.000Z","display":"6:15"}, {"date":"2014-12-26T05:30:00.000Z","display":"6:30"}] When I use the following code: <select ...

outcome of not making it into the industry

In this code snippet, I have three fields where the sum of two values entered should automatically populate in the third field. However, it seems like the sum function is not working as expected. Here's the code I'm using: <script type="text/ ...

Is it possible to utilize $.each() in combination with $.ajax() to query an API?

I am dealing with an array containing 2 values, and for each value, I need to make an AJAX query to an API to check the stock availability. If there is stock for both values, a certain message should be executed, otherwise, a different message. This check ...

Can Datatables be incorporated with modal pop-ups for editing purposes?

I am dealing with a DataTable that fetches data from a remote server. Each cell in the table has a mouse-over link that can trigger a modal popup (all links are the same). The issue I'm facing is the inability to pass the unique ID of the row and the ...

Successive, Interrelated Delayed Invocations

There are two functions in my code, getStudentById(studentId) and getBookTitleById(bookId), which retrieve data through ajax calls. My ultimate goal is to use Deferreds in the following sequence: Retrieve the Student object, Then fetch the Book Title bas ...

Choosing read-only text fields using JQuery

I am working on an ASP.NET MVC project and I am looking to select all text boxes with the "readOnly" attribute on the current page. Once identified, I want to trigger a modal jQuery dialog on keypress for each of these disabled text boxes. Any suggestion ...

Paths of least resistance: Absolute URLs for assets in CSS

What could be causing the absolute path for this asset (the font) not to work while the relative path does? <body> hello friend </body> <style type="text/css"> @font-face { font-family: 'Gilroy'; /* src: ur ...

Having issues with the right margin in HTML5 canvas and struggling to remove the scroll bar

I am struggling with adjusting the margins of my canvas to ensure equal spacing on both sides without any horizontal scroll bars. Desired Outcome: Equal margin on both sides of canvas without any horizontal scroll bars. Issue: The margin-right property i ...

The target for ajaxSubmit is being duplicated instead of being replaced

I encountered a problem with the code below: $('#refresh').click(function () { alert($('.report-container').length); $('.report-container').each(function () { var accordian = this; var url = $(this) ...

Using jQuery to iterate over a multi-dimensional array and showing the child elements for each parent array

I am facing an issue with a multidimensional array that contains objects and other arrays. While it is simple to loop through the parent array and display its contents in HTML, I encounter a problem when there are multiple layers of arrays nested within ea ...

Changing the structure of divs by using elements from different divs

I'm looking for some help with adjusting the CSS of a div when hovering over certain elements. Here is my current code: <div class = "container-main"> <div class = "container-inner"> <ul class = "list"> &l ...

Organizing an angular expansion panel

I am currently dealing with an expansion panel that has a lot of content. The layout is quite messy and chaotic, as seen here: https://ibb.co/Y3z9gdf It doesn't look very appealing, so I'm wondering if there is a way to organize it better or ma ...

Strikeout list on click of a mouse

Is there a way to apply strikethrough formatting to text with just a mouse click? The CSS for lists is beyond the form field margin. I've tried multiple methods without success. No matter how many times I change the code, I can't seem to get it r ...

Display identical text using JavaScript filter

My search filter highlight is currently displaying [object Object] instead of <mark>match values</mark> when replacing the values. This is the code I am using: this.countries.response.filter((val) => { const position = val.value.toLowerCa ...