When a user clicks on the div, it expands in size and then returns to

I developed a small quiz, where clicking on an answer moves the checkmark to the clicked div.

const answers = document.getElementsByClassName('answer');
[].forEach.call(answers, function(answer) {
  $(answer).click(function() {
    if (!answer.querySelector('.check')) {
      [].forEach.call(answers, function(answerToDelete) {
        if (answerToDelete.querySelector('.check')) {
          answerToDelete.querySelector('.check').remove()
        }
      })
      $(answer).last().append('<div class="check"><object data="check.svg" width="20" > </object></div>')
    }

  })
})
.container {
  padding: 5%;
  display: grid;
  gap: 20px;
  grid-template-columns: 1fr;
}

.question_title {
  font-size: 18px;
  font-weight: 500;
  text-align: center;
}

.container_answers {
  display: grid;
  justify-items: center;
  gap: 10px;
}

.answer {
  display: flex;
  align-items: center;
  border: 1px solid black;
  padding: 0 10px;
  max-width: 15%;
  min-width: 170px;
  border-radius: 10px;
  cursor: pointer;
  overflow: hidden;
}

.dot_answer {
  background-color: black;
  color: white;
  margin-right: 7px;
  padding: 5px 10px;
  border-radius: 5px;
}

.text_answer {
  font-weight: 700;
}

.check {
  margin-left: 20%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<div class="container">
  <div class="question_title">
    <p>I am a Question, what is your answer?</p>
  </div>
  <div class="container_answers">
    <div class="answer">
      <span class="dot_answer">A</span>
      <p class="text_answer">Answer 1</p>
    </div>
    <div class="answer">
      <span class="dot_answer">B</span>
      <p class="text_answer">Answer 2</p>
    </div>
    <div class="answer">
      <span class="dot_answer">C</span>
      <p class="text_answer">Answer 3</p>
    </div>
    <div class="answer">
      <span class="dot_answer">D</span>
      <p class="text_answer">Answer 4</p>
    </div>
  </div>
</div>

However, when I click on the div, it expands and shrinks back instantly. I tried using overflow:hidden, but it didn't work. The transition between the checkmarks should be smooth.

Answer №1

This JQuery script is designed to work only when an image is present.

$(function(){

 const reps = document.getElementsByClassName('rep');

    [].forEach.call(reps,function(rep){

        $(rep).click(function(){

        // This section removes the Check Div for the old Check
        [].forEach.call(reps,function(repToDel) { if(repToDel.querySelector('.check'))  repToDel.querySelector('.check').remove(); });

        // This section adds the Check Div for the new Check
        if(! rep.querySelector('.check'))   $(rep).last().append('<div class="check"><object data="check.png" width="20" > </object></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

When using sequential jQuery 'pages', an error referencing the third frame occurs

I am new to using javascript/jquery and have been experimenting with the w3schools tutorials and jquery documentation. I created a page where user input is accepted and javascript prints output based on that input. I tried modifying it to work sequentially ...

Align a single item to the right in PrimeNG p-menubar

I am currently utilizing PrimeNG 8.1.1 and I am looking for a way to align one of the items to the right side (specifically the submenu options of logout and profile). Does anyone have any suggestions? this._menuItems = [ { labe ...

Divs that are 30% of their parent's width will not align vertically and will not fit in a parent div that is 100% width

I am attempting to vertically center the three child-divs <div class="section"> as 3 rows in one column within <div class="container_page_2">. When I refer to vertical alignment, I mean having an equal distance between the top of the page and ...

Browsing through files on IE8 within one window

I recently encountered an issue that I have never experienced before. Typically, when browsing through my folders in Internet Explorer, everything worked smoothly for me. If I included various directory links, I could easily navigate back and forth to expl ...

Refreshing a view in Laravel after a successful insertion using JQuery Ajax

I have been successfully inserting records into a table using jQuery ajax, and receiving a flash message confirming the successful insertion. However, I am now facing an issue where I do not know how to reload the table to reflect the changes after the rec ...

Implementing this type of route in Vue Router - What are the steps I should take?

I am currently delving into Vue and working on a project that involves creating a Vue application with a side menu for user navigation throughout the website. Utilizing Vue Router, my code structure looks like this: <template> <div id="app ...

Is there a way to pre-load images from component B within component A using Vue?

In a scenario where I have two Vue files, A.vue and B.vue, the issue arises when navigating from A to B. B contains numerous images fetched from Firebase realtime database, resulting in slow loading times upon first visit. To address this, I aim to preload ...

Using the .map method to index an array is causing issues in Node.js

Hey everyone, I'm struggling to properly index a JSON array using the map function. It seems like my code isn't quite right. This is what I have: var entireHTMLssssq = lloopmois.map((result, index) => `<div id=${index} style="position: a ...

Objects in Three.js Sparkling from Afar

Currently, I am tackling a project that involves working with scenes containing objects ranging from 10 to 1000000 in size. A recurring issue I have encountered is that when dealing with the larger end of this size spectrum, the objects seem to 'shimm ...

Creating multiple Google markers based on addresses retrieved from a database using PHP

I have a requirement to display multiple Google markers on a map based on addresses stored in a database. I have already successfully loaded an XML file to retrieve latitude and longitude coordinates, but now I need assistance on how to create multiple m ...

Generate an array using the properties of an object

Seeking a more efficient way to configure settings for an app using objects? Consider starting with the following object: var obj = { property_one: 3; property_two: 2; property_three: 1; } And transforming it into the desired array format: v ...

Vue.js encountered an error: Unexpected TypeError in promise. The function $set is not recognized

Currently, I am working on fetching comments from the Reddit API and attempting to update an array using $set in order to refresh the view. However, I encountered an error: Uncaught (in promise) TypeError: $set is not a function Virtual Machine Component ...

Accessing JSON data in Node.js can be accomplished using various methods. By leveraging

Currently, I am attempting to retrieve data from a JSON file using nodeJS However, upon running the code, I encounter the following error message: TypeError: Cannot read property 'postcode' of undefined. Any ideas on how to resolve this issue? ...

Reinitializing an array in JavaScript

Encountering a strange issue when attempting to reset an array. For example: data.length=0; This behavior is puzzling. I am populating the array with updated values on each iteration of my program. The array is then used in another function. However, upo ...

The token endpoint in Nuxtjs auth module's configuration for auth strategies is not being triggered

Our system has two important endpoints, namely /auth and /token. The endpoint /auth is responsible for providing the authorization code required to call /token in order to obtain an access token. The utilization of NuxtJS has made the auth module a prefer ...

What is the best way to duplicate the table header?

I'm looking to have the table header repeat twice using ng-repeat, like this: a b | a b | Instead of aa | bb. Currently, my code only displays a b | <table class="table table-striped table-bordered"> <thead> ...

Press the "Load More Categories" button in Wordpress to display additional categories

I am looking for a way to implement a "Load More" button on my website. Currently, I am using the get_categories function to display categories on my TV show website. Each category represents a series and the posts within each category are episodes. You c ...

Guide on adding a unique ID for every table by inserting a div

I am currently working on a website that contains multiple tables on each page. My goal is to implement a scroll bar at the top, customized for each table. After searching online, I came across this helpful solution: http://jsfiddle.net/simo/67xSL/ I have ...

What steps can you take to ensure that a single-page-application (SPA) is easily searchable within a SharePoint environment

In the process of developing a single-page-application (SPA) with ASP.NET MVC, knouckout, and various other libraries, we have decided to handle routing on the front-end, potentially utilizing crossroads.js. Our use of slickgrid.js allows us to present a w ...

Joomla, Enhancement for iFrame Sizing

A close associate who runs his own construction business has asked me to create a website for him using Joomla. I have successfully implemented most of the features he wanted, but now I need some assistance. He wants to showcase references written about hi ...