Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7612643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:57:57+00:00 2026-05-31T01:57:57+00:00

Currently what I have coded up works, but it seems rather crude. Basically here’s

  • 0

Currently what I have coded up works, but it seems rather crude. Basically here’s what I have (simplified):

JQuery ready:

<script type="text/javascript">

    $(document).ready(function() {
        $('div.partDelete').click(function() {

        // TODO this seems like a dirty hack
        var split = this.id.split('_');
        if(split.length == 3) {
            $('#part_id').val(split[0]);
            alert($('#part_id').val());
            $('#removePartForm').submit();
        } else {
            alert('There was a problem removing the selected part');
        }
    });

</script>

The form I am using:

<form id="removePartForm" action="{% url remove_part %}" method="post">
    <input type="hidden" id="part_id" name="part_id" value="-1" />

    {% for part in current_build.parts.all %}
        <div id="{{ part.id }}_part_id" class="partDelete">remove</div>
    {% endfor %} 
</form>

All I’m trying to do is set the hidden input to take the part.id that has been selected by the user so that I can use it in my views.

For all I know this is the correct way to go about this, but I just have a feeling it isn’t. I am new to Django & JQuery so there may be some built in functionality for this that I haven’t found yet.

Thanks for any suggestions that you may have!

Solution (see mikaelb’s answer below)

Javascript:

$('div.partDelete').click(function() {
    var selected_id =$(this).data("id");
    $('#part_id').val(selected_id);
    $('#removePartForm').submit();
});

HTML changes:

<div class="partDelete" data-id="{{ part.id }}">remove</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:57:58+00:00Added an answer on May 31, 2026 at 1:57 am

    First of all; IDs shouldn’t start with a number (http://www.w3.org/TR/html4/types.html#type-name).

    Aside from that, you’d typically use the data-* attribute for setting IDs to communicate with JS from server side. The data-* attribute can be anything you want. So data-foo=”” is a valid attribute.

    Example:
    HTML:

    <div class="item" data-id="{{ part.id }}">
        <p>Foo</p>
    </div>
    

    Javascript:

    $(function () { // same as document read
    
        $(".button-class").on("click", function () {
            // Here "this" will be the element
            var id = $(this).data("id"); // same as getting attribute data-id
            // Could also use $(this).attr("data-id")
        });
    });
    

    Hope this helps.

    Edit: Moved comment to make it more clear, changed the example to be more specific to OP’s problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to get the local date/time. It works but it seems
I have a little bit of Javascript that almost works correctly. Here's the code:
There is probably is simple fix for this but I currently have code similar
I have some Python code that works correctly when I use python.exe to run
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have code in my ApplicationController to check if a user is logged
I currently have code that does the following: private final static ExecutorService pool =
Using dotnet 2.0. I currently have code like this : DataView dv = new
I currently have some code that will produce a crash dump when my application
The offending block of code is below. The code almost always works, but sometimes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.