The drop-down does not move above the subsequent div when focused

I have implemented a dropdown feature on focus of an <input type='text'> box in the following manner

http://jsfiddle.net/Newtt/7ffdF/

The issue I am facing is with the positioning of the dropdown box. It currently displaces the content of the container-box below it. Is there a way to make this text box act like a select tag without actually using the select tag?

My CSS for the dropdown div includes:

display:none;z-index:200;

To show the dropdown on focus, I use the following jQuery code:

$('#text-box').focus(function(){
    $('.dropdown').show();
});

Additionally, I want the dropdown to disappear when the focus is removed from the search box.

In summary, I have two questions:

  1. About fixing the position of the dropdown
  2. Enabling toggling of the dropdown visibility on and off focus of the search box

Thank you!

Answer №1

To create a dropdown, you can utilize the CSS property position:absolute;. Additionally, implement the Jquery event blur on the text field to hide it.

Check out the JS FIDDLE DEMO here

JS

$(document).ready(function(){
    $('#text-box').focus(function(){
        $('.dropdown').show();
    });

    $('#text-box').blur(function(){
        $('.dropdown').hide();
    });
});

CSS

.container-box {
    height:300px;
    width:300px;
    background: #eee;
}

.dropdown{
    display:none;
    position:absolute;
    z-index:100;
}

.dropdown ul {
    padding :0;
    background : #aaffdd;
}

The styling of the drop-down is left to your creativity! Customize it as you see fit :)

Answer №2

To prevent the dropdown from affecting other elements, you can utilize position: absolute; in CSS.

If you want to handle what happens when there is no focus, you can use the jQuery method .blur().

Check out this demo on JSFiddle: http://jsfiddle.net/7ffdF/12/

CSS:

.dropdown {
    display: none;
    z-index: 200;
    position: absolute;
    background-color: #eee;
    width: 400px;
}

jQuery code:

$('#text-box').blur(function() {
    $('.dropdown').hide();
});

Answer №3

To position the div absolutely, you can follow this CSS code:

div.dropdown{
    width:170px;
    border:1px solid #ccc;
    position:absolute;
    background-color:#fff;
}

For a visual demonstration, check out this JS Fiddle link

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

When attempting to incorporate Jasmine into my current AngularJS/Bootstrap project, I encountered the error message stating

Currently, I am working on a group project that requires test coverage. The project utilizes npm and angularJS. To kick things off, I performed an npm install for jquery, jasmine, and angular-mocks. Since I'm relatively new to testing, I decided to st ...

I am looking to create a split div with a diagonal line running through

Is there a way I can create this Mockup using html5 and css3, but also add diagonal lines to divide the div? Any suggestions on how to achieve this? ...

Accessing information from Firebase and displaying it within an Angular Controller

As a newcomer to the latest Firebase SDK (with some experience using angularfire), I set out to retrieve data and display it using Angular. This is my progress so far: var app = angular.module('dApp',[]); app.controller('listingControler&a ...

Navigating to Protected Mobile Platform

I am experiencing an issue with accessing a .NET website with SSL on my Blackberry device. When I try to view the site, I receive the message "HTTP ERROR 403 FORBIDDEN - You're not authorized to view this page. Please try loading a different page". Th ...

AngularJS: Issue with watching arrays and deep $watch functionality

I'm having trouble using the $watch function in AngularJS with an array of boolean values. I want to display a message when there's a change in the array, but it's not working as expected. Check out this code example where the array values ...

The HTML grid is causing an irritating excess space on the right side of my website

After brainstorming an idea, I decided to create this website for testing purposes. However, the grid layout seems to be causing an unwanted margin on the right side of the page that is not associated with any HTML tag, disrupting the zoom functionality. ...

Tips for sending custom props to a dynamic page in Next.js

I have created a component called Card.js which is responsible for linking to dynamic pages when a card is clicked. My goal is to pass a value, such as 'category', to the dynamic page [id].js so that I can implement additional logic there. Card. ...

Error: Reactjs - Attempting to access the 'name' property of an undefined variable

As I continue to learn about React, I have been experimenting with props in my code. However, I encountered an error where the prop is appearing as undefined. This issue has left me puzzled since I am still at a basic level of understanding React. If anyo ...

Retrieving Data from a JSON Object Using a Specific Key

Received a JSON response similar to the one below { "SNGS": { "$": { "xmlns": "csng", "xmlns:ns2": "http://www.w3.org/1999/xlink" }, "Defec ...

Node.js is throwing an error "Unexpected token v in JSON at position 0" which

I'm currently working on developing PHP code within the Google App Engine flexible environment. Specifically, I am facing some challenges in setting up the Firebase SDK Admin for the web version. My main struggle lies with the following block of code ...

Tips for adjusting the close date based on the selected date in a dropdown datepicker

Here is a sample code snippet: <input id="date1" name="start_date" id="dpd1"/> <select class="form_line_only form-control" name="ho_night"> <option selected> 0 </option> <option ...

Sharing information between pages in React through Router

I am struggling to transfer a single string from one page to another using react router and only functional components. I have created a button that links my pages, but I can't seem to pass the string successfully. Here is an example of the code on th ...

updating the HTML DOM elements using JavaScript is not yielding any response

One way that I am trying to change the background of a div is by using a function. Below is an example of the html code I am working with: $scope.Background = 'img/seg5en.png'; document.getElementById("Bstyle").style.background = "url("+$scope.B ...

The full background width is not being displayed as 100% on the

Unexpectedly, the "full left secondary-bg" background division on this particular page (http://goo.gl/OU4MkW) is no longer spanning the full width of the screen and I am unable to figure out why. This website is WordPress-based and constructed using the sk ...

Incorporate a new style into several previous slides using the Slick carousel feature

I'm attempting to utilize the Slick carousel to create a timeline. My goal is for the previous slides to remain colored as you progress through the timeline, and turn grey when you go back. I've tried using onAfterChange and onBeforeChange, but I ...

Leveraging jQuery to send data to MySQL with the click of a button

I am attempting to utilize jQuery in order to post a variable to a MySQL/PHP query. The button with the class="button" has the following code: $(".button").click(function(e){ e.preventDefault(); $.post("some.php", { var_today : <?php echo $new_var; ...

Using jQuery Datepicker Beforeshowday to dynamically retrieve an array of unavailable dates through Ajax

Currently, I am working on implementing a datepicker that will update an array of unavailable dates. While it successfully works with a PHP variable being passed, the challenge lies in properly returning the data for the option (as it is throwing a console ...

After a duration of 4 minutes, an ERR_EMPTY_RESPONSE is thrown in response to the

My angular 5 node app involves sending a request to the server and waiting for a response. There are instances where the response takes around 5-6 minutes to arrive. However, an ERR_EMPTY_RESPONSE error is triggered by the http method after just 4 minutes ...

Adjust the ZIndex on a div through interactive clicking

For my new project, I'm exploring a concept inspired by Windows 7. The idea is that when you double click on an icon, a window will open. If you keep double clicking on the same icon, multiple windows should appear. The challenge I'm facing is im ...

Interactive Infographics in HTML5 format

Currently, I am unable to find any examples on how to accomplish a specific task mentioned in the following link. If you have a tutorial with step-by-step instructions or a code project available, it would greatly assist me. Link: Evolution of WEB ...