Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
comparing value from inputfield with django model data in js function

I have already made an app that allows me to study. It contains multiple test with multiple question with one correct answer each.


class Test(models.Model):
    name = models.CharField(max_length=200)
    #questions = models.ManyToManyField(Question)
    author = models.ForeignKey(User, on_delete=models.CASCADE, default=None, null=True, blank=True)
    date_posted = models.DateTimeField(auto_now_add = True)

    def get_questions(self):
        return self.question_set.all()

    def __str__(self):
        return self.name

class Question(models.Model):

    text = models.CharField(max_length=200, null=True)
    test = models.ForeignKey(Test, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add = True)

    def get_answer(self):
        return self.answer_set.all()

    def __str__(self):
        return self.text

class Answer(models.Model):
    text = models.CharField(max_length=200)
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='parent')
    def __str__(self):
         return self.text

I made JS funtion that compare user answer from input field with correct answer put in html

function PointsCounter(){
   var points = 0;
   const elements = document.querySelectorAll('.question-container');
   Array.from(elements).forEach((element, index) => {
      var CorrectAnswer = (((element.querySelector('div form p')).innerHTML).toLowerCase()).trim();
    
    var UserAnswer = (((element.querySelector('div form p input')).value).toLowerCase()).trim();

   if(UserAnswer == CorrectAnswer){
   points++;
   }})
    document.getElementById("points").innerHTML=points;

                    
    }

Instead of putting correct answer in my html code:

<p id="ans" hidden>{{ question.parent.all.last }}</p>

I'd like to compare user answer with answer from database, but I do not know how to achieve it.

I mean, i'd like to pass correct answer to js function to disallow user to check answer by inspecting html code of page. How can I make it ?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!