Remove every element from the div except for a select few

Imagine having a container with various elements like the ones below.

<div id="ans" style="background-color: #FFD993;
    color: #FFF;
    overflow:auto;
    border: 1px outset #4f6417;
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;width:100%;text-align: center;height:33%;margin-bottom:    0px;">
    <input type="button" id="again" value="See Again"></input>
    <input type="button" id="prac" value="Practice"></input>
    <img id="theImg" src="result/' + rand + '.png" width="40%" height="60%"/>
    <a href="1.html">1</a>
    <a href="2.html">2</a>
    <p>This is Paragraph</p>
</div>

I would like to remove all the elements inside this div except for the two buttons. I have tried using:

$('#ans').empty();

However, this method clears out all contents. Is there a way to specifically filter the removal?

Answer №1

What if we only keep the input elements and remove all other children?

$('#ans').children(':not(input[type="button"])').remove();

Check out this FIDDLE for reference.

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

React Alert: __WEBPACK_IMPORTED_MODULE_4_jquery___default(...) is throwing an error and modal function is not working

I've been working on a React project that utilizes Bootstrap 4, which I've imported through a CDN. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> <script src="https://ajax.googleapis ...

Error encountered when auto-generating IDs in Next.js with Firebase Firestore database

Hello, I am trying to access the details of my page in Next.js and retrieve data from a Firestore database. Here is the error message I am encountering: Firebase initialized successfully page.js:32 router.query: undefined page.js:34 Movie ID: undefined p ...

Execute JavaScript AJAX Request On Page Load

I am facing an issue with creating an onload event in which an AJAX function needs to be called and passed a value. The challenge lies in the fact that I have both my header and footer split into sections via PHP, therefore I cannot place it on the body ta ...

Modify the parent window's URL with the help of fancybox

In my recent project, I am facing an issue. I have a gallery page called gallery.php using PHP which includes jQuery fancybox to display each photo in the gallery as a pop-up. Additionally, I have a separate page details.php to show individual photo detail ...

When the width of a tr tag is reduced, an <select> and <span> tag will split into two lines

I am working on creating a table that includes a field for each row along with a symbol to indicate mandatory fields. As the screen width decreases, I want the content to break into a new line at a specific point. Desired behavior: The field width should ...

Invoke PHP by clicking on a button

I am facing an issue with a button I have created. Here is the code for it: <input type="submit" name="kudos_button" value="★ Give kudos"/>' To test it, I wrote a PHP script like this below the </html> tag: ...

The Ajax request returns a 200OK status code indicating success, yet the response payload is missing

Recently, I attempted to utilize a basic password strength-checking service. However, despite receiving a successful ajax call with a 200OK response status, the response body was empty. The error callback provided below was triggered instead: var password ...

I encountered a crash in my app because of an error in my Node.js backend code that was posting the accessories and slug into the database

My node.js backend code is responsible for adding the accessory and slug to the database, but I am encountering app crashes. const express=require('express'); const Category = require("../models/Category"); const slugify=require('s ...

Tips for toggling Bootstrap 5 tabs using JavaScript instead of the button version

I am looking to switch tabs programmatically using bootstrap 5. The Bootstrap documentation recommends using buttons for tabs instead of links for a dynamic change. Here is the code I have: $("#mybut").click(function() { var sel = document.querySelector( ...

The ajax keypress event is malfunctioning and the ActionResult parameter is failing to capture any data

I am facing an issue where I want to update the value of a textbox using AJAX on keypress event, but the controller is not receiving any value to perform the calculation (it's receiving null). <script> $('#TotDiscnt').keypress(fu ...

Need to specify the file type at the end of the URL for HTML input

How can I create a URL input form that requires the input to be a valid URL and end in a specific file type? Here is an example of my input: <input name="bg" placeholder="https://website.com/image" type="url"> Currently, the input field only restr ...

What causes background images to be affected by collapsing margins?

I am working with simple HTML and CSS code. One issue I have noticed is the bottom margin collapse between the .outside box and the .inside box. I am puzzled as to why the background image does not show when this bottom margin collapses, as the backgroun ...

Custom ID in Firebase Firestore is not generating a new document even after successfully creating a new user

In my Vue App, I designed a registration form to automatically create a Firestore document linked to the User UID with a unique document ID. Although the user creation process is successful, the document creation fails without showing any errors in the c ...

Adding a fresh dependency to the current package.json file

Introduction: I am relatively new to JavaScript and have encountered a basic issue regarding adding dependencies to an existing JavaScript project. Despite double-checking the installation instructions, I seem to have encountered some errors in my terminal ...

Setting the OrbitControls Position in ThreeJS

Currently, I am saving the position of the OrbitControls by using .getAzimuthalAngle() and .getPolarAngle(). In the future, I would like to update these angles with slightly different values. I chose to utilize getAzimuthalAngle and getPolarAngle because ...

Leveraging jQuery to extract numerous concealed data from a webpage based on their names

Here is the scenario: <input type="hidden" name="uID" id="uID" value="251|0"> <input type="hidden" name="uID" id="uID" value="252|0"> <input type="hidden" name="uID" id="uID" value="253|0"> <input type="hidden" name="uID" id="uID" val ...

What is the best way to pass JavaScript object literals from a Node.js server to the frontend browser?

In Node, I am working with JavaScript object literals containing methods in the backend. For example: const report = { id: 1, title: 'Quarterly Report for Department 12345', abstract: 'This report shows the results of the sales ...

JavaScript - Global Variable

Is it possible to declare and assign a value to a global variable in one JavaScript file and then reference it in another JavaScript file multiple times? Can Angular.js be utilized in a Velocity macro file, aside from HTML files? ...

Simple Steps to Connect an HTML File to CSS in Windows Notepad

I have scoured the depths of the online realm in search of a method to connect HTML with CSS within the preinstalled Notepad application on Windows. Regrettably, my attempts, including utilizing the system path, proved fruitless. Is it conceivable to ach ...

Need a tool for validating forms?

I am currently facing some confusion with the UI Validation plugin that we are using. Within our application, we employ Spring MVC, Jquery & Bootstrap. As we delve into UI development, we have encountered uncertainty in selecting an appropriate Validation ...