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

160
Views
Why I am getting "HTMLButtonElement.<anonymous>" while passing my django views data in javascript fucntion?

I am all new to javascript. I was working on a Django project to make a social networking app. I am having a problem with the like-button that will update the colour of the like button and the number of likes asynchronously without reloading the page. Here I'm sharing my Django template having the like button's code.

<form action="{% url 'likepost' posts_id=posts.id %}" id="likeform" method="POST" style="display: inline;">
        {% csrf_token %}
        <button id = "like" class="btn btn-link">Like</button>
    </form>
    
    <small id="num_of_likes">{{ posts.likepost.all.count }}</small>

Through the button containing id = "like", my javascript code will receive the Django template variable and fetch API from Django. Here is my js code.

document.addEventListener("DOMContentLoaded",function(){
    document.querySelector('#like').addEventListener('click', ()=> like_function('like'));
})


function like_function(like){
    fetch(`/index/${posts_id}`,{
        method:"POST",
        body : JSON.stringify({
            "is_like" : is_like,
            "num_like" : num_like,
        })
    })
    .then(response => response.json())
    .then(result => {
        if(result.is_like){
            document.querySelector('#like').innerHTML = "Unike";
        }
        else{
            document.querySelector('#like').innerHTML = "Like";
        }
    })
}

Although my Django views method returns JSON data, I want my js code will get that JSON data and returns back to my homepage. Here's what error I got: enter image description here

I didn't get what was actually going on. I want my javascript method will take me back to my homepage. Here's I have seen what's I am actually facing off. https://youtu.be/PV6ctAQNA64

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!