How can I remove a div block if it has been selected?

Is there a way to remove a div from the page when a specific item is selected in a select box, rather than just hiding it?

 <select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option id="remove" value="mercedes">Mercedes</option> if this option is selected ?
  <option value="audi">Audi</option>
</select> 

<div id="remove">How can I remove this div completely?</div>

I'm unsure how to delete a div block when a certain option is chosen. Can anyone help?

Answer №1

Please ensure that IDs are unique. Instead of using "id", use a different attribute to identify the element that needs to be removed

$(document).ready(function(){
  $('select').on('change',function(){
    if($(this).val() == 'mercedes'){
      $(this).next('div#remove').remove();
    }else{
      $(this).next('div#remove').remove();
      $(this).after('<div id="remove">how to remove this div ?</div>');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 

<div id="remove">how to remove this div ?</div>

Answer №2

To easily achieve this functionality, utilize the power of jquery. Simply set up a listener for when the dropdown menu changes. If the selected value is mercedes, then you can remove the <div> element with the ID of remove. Adding an ID to the <select> tag helps prevent conflicts with other dropdown menus on the page if you choose to use multiple in the future. Alternatively, you can use:

$('select').change(function(){

to replace:

$('#cars').change(function(){
$(document).ready(function(){
   // Listen for dropdown change
   $('select').change(function(){
      if($(this).val() === 'mercedes'){
        // Remove the specified element from the DOM
        $('#remove').remove();
      }
   });
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='cars'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option> What happens when this option is selected?
  <option value="audi">Audi</option>
</select> 

<div id="remove">How do I remove this 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

Tips for choosing a single row in a table using a checkbox

I need help with a table that has multiple rows and four columns. One column is for checkboxes, while the other three are select boxes set to read-only. I want users to be able to edit only one row at a time by checking the checkbox in the first column. If ...

What is the best way to transfer a jQuery variable to PHP variables in order to use them in a Select Query?

<script type='text/javascript'> $(window).load(function(){ $("body").click(function(){ var s = window.getSelection(); s.modify('extend','backward','word'); ...

What are the negative effects of placing an external CSS stylesheet link outside of the <head>

At the outset, it is well-known that the <link> tag used to connect an external CSS style sheet should ideally be placed within the <head> section of an HTML document. It is considered unconventional to place it elsewhere. However, due to my u ...

Retrieving data from a JavaScript variable beyond its designated function

Is there a way to access the JavaScript variable named locations outside of its assigned method in the code below? Javascript code: $(document).ready(function() { var locations; console.log('document ready'); function initializeMap( ...

Activate the datepicker in Angular by clicking on the input field

This is my html file: <mat-form-field> <input matInput [matDatepicker]="picker" placeholder="Choose a date"> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-datepicker #picker></mat-date ...

The unique identifier for a specific child element in Firefox

While working on this website: I customized the right sidebar navigation using: .sidenav a:after { display: block; position: relative; z-index: 0; top: -3.6em; left: 0.15em; width: 90%; content: "."; font-family: 'R ...

Using jQuery to select all input fields on focus

When a user focuses on the field, I am attempting to select all of the text using this code. However, what actually occurs is that it selects all for a moment, then it becomes unselected and the typing cursor remains where I clicked... $("input[type=text] ...

The management of jQuery events through .on and .off functions, maintaining the correct order, and ensuring their

Check out this jsFiddle example: http://jsfiddle.net/fThMa/2/ By clicking inside the note or rend text fields and then double clicking any of the 4 TDs below, you can insert the appropriate HTML entities into the note or rend text fields. This also includ ...

My function is named, however, the output is recorded prior to the function completing its execution

I've implemented a function named createUser, designed to save user data in the database. If successful, it should return true; otherwise, false. The code for this function is as follows: exports.createUser = (user) => { const salt = crypto.rando ...

After an ajax request, Intersection Observer is incorrectly detecting the number of intersecting images

UPDATE: After refining my functions, I have made necessary adjustments in this post I am looking to incorporate lazy loading for images into a website. These images are part of various galleries that load asynchronously based on the user's selection. ...

A tailor-made Angular filter designed to exclusively show items falling within a specific date range

Hi there! I am currently delving into AngularJS and working on an application that pulls articles via JSON. My goal is to display items within a specific date range when a button is clicked. While I have the logic figured out, I am unsure of how to approac ...

Using React Native to assign state to a constant variable

As I venture into the world of react native, I encountered an interesting scenario while utilizing a library called react native paper. In the code snippet below, you can observe that the state is being assigned to a const. import * as React from 're ...

Disappointed with the outcome of the Masonry jquery implementation

Currently, I am exploring the possibilities of using Masonry for my website design. I aspire to achieve a layout as sleek as 500px.com or something similar. Through experimenting with Masonry and following tutorials and advice from this forum, I have gaine ...

Output the current screen to the console using the Stack.Navigator component in React Native

Trying to find a way to automatically log which screen is being displayed on each view when using the Stack.Navigator component in React Native with TypeScript. Adding console logs manually to every screen is too time-consuming for me. I am considering im ...

What is the method for locating line spacing within HTML code?

After posting a previous question, I am still on the quest to determine the exact position of each line of text within an element. While I was successful in identifying the css lineHeight attribute (as mentioned in the previous response), I encountered an ...

Adjust the standard size of the IMG tag using CSS

I have a simple image tag with an incorrect URL http://jsbin.com/monivava/1/edit <img src="http://placekitten!!!.com/g/200/200"> The issue is that the default size for this unsuccessful image is 18x20 (at least in Webkit), but I want it to be 0px ...

JQuery along with Font Awesome: Setting the default value for star rating

The Situation: I am currently working on a system where users can rate various products using Laravel. Each product has an average rating which is then used to display stars. These ratings are shown on the "Product Listing" page, which contains multiple p ...

Enter data into a static grid using asp.net

Currently, I am working on developing a data entry application and I have a vision for how I want it to be designed and operate. Specifically, I envision a static grid where users can click on cells to make edits. I have included an image showcasing this l ...

Locate the target item that shares the identical class

My goal is to achieve the following: When I click on a month #month > li >a, it should fade in the list with the class name of that month and hide the other month lists so only one is shown. While this may sound simple, I am unsure of the correct jQu ...

Working with HTTP Requests in Node.js

Having a good understanding of how HTTP requests work, I am able to send and receive responses. Below is an example code showcasing an HTTP request: var http = require('http'); var options = { host: 'www.nodejitsu.com', path: &apos ...