Reversing the order of input-group-addon and input in bootstrap on mobile devices

I attempted to adjust the layout of a bootstrap input-group-addon on mobile devices by using two input elements and manipulating their display and visibility properties. From a design standpoint, I achieved the desired result as the input is now positioned behind the addons ... however, there seems to be an issue with the Javascript code that is supposed to run on #input-newsearch when accessed on mobile devices. It appears that the script is still referencing the first input element. What could be causing this problem and how can I resolve it?

HTML:

<div class="input-group">
<input id="input-newsearch" class="form-control input-newsearch-desktop" type="text">                                                                                                                                           
<span class="input-group-addon" id="delete-newsearch">Delete</span>
<span class="input-group-addon" id="remove-newsearch">Remove</span>
<input id="input-newsearch" class="form-control input-newsearch-mobile" type="text">                                                                                                                                                    
</div>  

CSS:

.input-newsearch-mobile {
display: none;
visibility: hidden;
}   

@media (max-width: 768px){
.input-newsearch-desktop {
    display: none;
    visibility: hidden;     
}
.input-newsearch-mobile {
    display: initial;
    visibility: initial;        
}           
}

Javascript:

$('#delete-newsearch, #delete-newsearch').on('click', function() { 
$('#input-newsearch').val('');                                          
}); 

$('#input-newsearch').autocomplete({
source:'/source.php',  
minLength: 3,
...
});

Answer №1

It's better to use classes instead of Id's for your selection process.

The issue you're facing is because Id's should ideally be unique and when you have multiple elements with the same Id, it creates problems when trying to reference them.

Styling

I have included the class input-newsearch

<div class="input-group">
<input id="input-newsearch" class="form-control input-newsearch input-newsearch-desktop" type="text">                                                                                                                                           
<span class="input-group-addon" id="delete-newsearch">Delete</span>
<span class="input-group-addon" id="remove-newsearch">Remove</span>
<input id="input-newsearch" class="form-control input-newsearch input-newsearch-mobile" type="text">                                                                                                                                                    
</div>  

Javascript

Replaced Id selector '#input-newsearch' with class selector '.input-newsearch'

$('#delete-newsearch, #delete-newsearch').on('click', function() { 
$('.input-newsearch').val('');                                          
}); 

$('.input-newsearch').autocomplete({
source:'/source.php',  
minLength: 3,
...
});

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

Encountering an issue with a loop in my jQuery function - any solutions?

Having encountered an issue with a select object not working in my form-building function, I resorted to using an ajax call to fetch data from a database table. While the network tab in Chrome shows the data being retrieved successfully, the console displa ...

Tips for efficiently transferring a retrieved object from App.js to a child component in ReactJS version 16 and above using asynchronous methods

TL;DR How can I pass a fetched object from App.js to a child component asynchronously? Do I need to wait for the data to be completely fetched before returning App.js? If so, how can I achieve this? In an attempt to create a dashboard using react-chartj ...

Displaying the value of a jquery variable in an HTML document

I'm tackling a problem differently today compared to yesterday, but my knowledge of jQuery and JavaScript is quite basic. My goal is to increment the transform value of a div every 5 seconds: <div style="transform: translateX(0px);" id="slide_ima ...

Disregard Cloudflare's Automatic RocketLoader feature for certain JavaScript scripts

After extensive research and failed attempts, I am seeking help to disable Cloudflare Rocketloader for a specific JavaScript file on my WordPress website. Specifically, I need to exclude the Automatic Rocket Loader for a particular .js file. I attempted t ...

How come my dimensions are not returning correctly when I use offset().top and scrollTop() inside a scrolling element?

Currently in the process of developing a web app at , I am looking to implement a feature that will fade elements in and out as they enter and leave the visible part of a scrolling parent element. Taking inspiration from myfunkyside's response on this ...

Eliminating the excess space at the beginning of dialogues

I'm struggling with removing the unwanted whitespace above the title in my Material UI dialog popup. Even after applying a background color to the title and content area, I still have a white margin at the top that I can't seem to eliminate. Doe ...

Converting .aspx to an Image using forms authentication in asp.net

I have been experimenting with the following code: public System.Drawing.Bitmap CaptureWebPage(string URL) { // Creating a hidden web browser to navigate to the specified page System.Windows.Forms.WebBrowser web = new System.Window ...

managing commitments in TypeScript

Is there a way to convert a promise into a string, or is there another method for handling this result? I am encountering an error stating "You cannot use an argument of type 'Promise' for a parameter of type 'string'." const pokemonIma ...

Bootstrap 5 and Vue 3 team up to create a stunning masonry layout

I've been struggling to find a proper solution for implementing a masonry layout in Vue 3 with Bootstrap. I tried using Bootstrap 5 cards with the Masonry CDN, but it resulted in squeezed and overlapping cards, failing to achieve the desired layout. I ...

What is the best way to combine two pieces of information from a constantly changing table?

Could really use some help with jQuery! I'm trying to calculate the cumulative value from columns A and B, then add those values together. Check out this image for the expected output. <script src="http://code.jquery.com/jquery-1.11.2.min.js"> ...

Utilizing the $set method to capture a jQuery variable and append it to a Vue array object

Currently, I am retrieving data from an API using jQuery's getJson method to extract the information. After successfully obtaining the data, my aim is to assign it to a Vue array object by making use of app.$set. Although I have managed to extract an ...

Looking for guidance on building a real-time React application with automatic data fetching linked to a nodejs backend

I have a simple question, I have developed an app that retrieves data from a backend server, Now, the app needs to be accessed and edited by multiple users simultaneously while ensuring that all changes made to the list are reflected in real-time for ever ...

How can I transmit both the $_POST and $_FILE variables to a .php file using Ajax?

I am looking to integrate an ajax image uploading system into my website for various reasons. I want the upload process to be seamless without having to refresh the page. While some jquery plugins have worked well for this purpose, I encountered an issue ...

Create a configuration featuring filter options similar to Notion's functionality

The objective is to create a system that can establish certain constraints, similar to the way Notion handles filter properties. https://i.sstatic.net/plctW.png System A sets up the constraints and System C evaluates them, both using Typescript. However, ...

Adjust the background hue and opacity when incorporating a "modal div"

Currently, I have a page where a hidden div appears at some point to display a form for inputting data. This could be considered a modal div. My goal is to darken the rest of the page and only allow interaction with this specific div. I want the backgroun ...

activating a jQuery function and sending a parameter

Looking for some help with JavaScript - I'm having trouble getting this code to work. I need to trigger a predefined function from one script within my AJAX script. Specifically, I want to activate the qtip functionality on the content of a div that i ...

Obtain scope in AngularJS using object ID

Is it possible to retrieve the specific scope of an object by accessing it through an ID? I am currently using angular ui tree for a navigation menu. However, I face an issue where after adding a subitem and saving the navigation into a mysql database, th ...

Is it possible for the NextJS Client component to only receive props after rendering props.children?

Encountering a puzzling issue that has me stumped... In my setup, I have a server side component that fetches data and then sends it over to the client component, which is all pretty standard. Here's where things get weird... When I log the data on ...

Array updating using the foreach method in Angular

Hey everyone, I've encountered an error that seems to be related to scope and I could use some advice. I'm currently looping through an array and trying to push the results to another array. However, when I attempt to push the results to public m ...

How to dynamically reduce the number of columns in a textarea using jQuery according to its content

Does anyone know how to make a textarea shrinkwrap the text inside on blur? The default number of columns is 10 or 15 and I want the textarea to adjust its width based on the text content. I have tried the following code: $('textarea').on(&apos ...