How to clean a form with stars in javascript after submitting?

Vladislav

There is simple form with stars which has 2 icons: star & star_border (star is for marked one and star_border is for an empty).

The form is very simple itself:

<div class="star-input">
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
</div>

When form being loaded stars constantly changing between marked and empty while visitor haven't pressed on it. As soon as he pressed on some star the status being frozen and some stars are filled while other are empty (depending on marks). So this way ranking being formed. It is clear for me.

However I try to make a solution to clear a form to initial state after it being submitted. If you open it after a submittion now with no reload a page there is still past value being stored.

Here is JavaScript's function code:

prepareThemes.prototype.initReviewDialog = function() {
  const dialog = document.querySelector("#dialog-add-review");
  this.dialogs.add_review = new mdc.dialog.MDCDialog(dialog);

  this.dialogs.add_review.listen("MDCDialog:accept", () => {
    let pathname = this.getCleanPath(document.location.pathname);
    let id = pathname.split("/")[2];

    if (
      !dialog.querySelector("#text").value ||
      !dialog.querySelector("#textwhoareyou").value
    ) {
      alert("No text");
      return;
    } else {
      this.addRating(id, {
        rating,
        text: dialog.querySelector("#text").value,
        userName: dialog.querySelector("#textwhoareyou").value,
        timestamp: new Date(),
        userId: firebase.auth().currentUser.uid
      }).then(() => {
        this.rerender();
      });
      dialog.querySelector("#text").value = "";
      dialog.querySelector("#textwhoareyou").value = "";
    }
  });

  let rating = 0;

  dialog.querySelectorAll(".star-input i").forEach(el => {
    const rate = () => {
      let after = false;
      rating = 0;

      [].slice.call(el.parentNode.children).forEach(child => {
        if (!after) {
          rating++;
          child.innerText = "star";
        } else {
          child.innerText = "star_border";
        }
        after = after || child.isSameNode(el);
      });
    };
    el.addEventListener("mouseover", rate);
  });
};

Thanks for any advice. Good day to everyone.

Amr Aly

Easy solution is to rebuild your div again after your form is submitted so You can do something like this:

 function doRate() {
  document.querySelectorAll('.star-input i').forEach(el => {
const rate = () => {
  let after = false;
  rating = 0;

  [].slice.call(el.parentNode.children).forEach(child => {
    if (!after) {
      rating++;
      child.innerText = 'star';
    } else {
      child.innerText = 'star_border';
    }
    after = after || child.isSameNode(el);
  });
};

el.addEventListener('mouseover', rate);
});
};

doRate();

document.querySelector('#my-form')
  .addEventListener('submit', function(e) {
    e.preventDefault();

    // Do you logic here
 
    // And after that rebuild your div        
    var myDiv = document.querySelector('.star-input');
    myDiv.innerHTML = `
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>`;
    
    doRate();
  })
  
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet">
<form id="my-form">
<div class="star-input">
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
    <i class="material-icons">star_border</i>
</div>
<button id="my-button">Rate!</button>
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I redirect users after successfully submitting form data?

How to avoid URL $_GET variables after submitting a form with ajax?

how to keep values in textfields after submitting a form in laravel 5.2

How to prevent the modal from closing after submitting form

How to display result from two states after submitting a form

How to redirect to another HTML page after submitting a form?

Pure Javascript Ajax request after submitting a form

Form clears after submitting

How to redirect user after submitting form with React

How can I empty this AngularJS form after submitting the data?

How to redirect website after submitting google form?

how to clear validation errors for mat error after submitting the form

How to get a popup box after submitting a form?

How to clean form after submitting ajax?

php - How to keep form values after submitting to another website?

How to redirect after submitting form in php

<a href='javascript:...'>is submitting a form

How to open another submit form after submitting one on html or javascript?

How to display alert message after submitting form

Form not submitting after form validation

submitting form on through <a> with javascript

How to reset a bootstrap modal after submitting the form in jquery?

How to clean form after wrong filling out?

How to load table data after submitting the form in react js php

How to close Window Tab After Submitting a Form in Symfony?

how to refresh the list after submitting a form in react-native?

How to redirect after submitting form

How to reset the form in React after submitting?

The page is getting reset on submitting form after a while in HTML, javascript

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive