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

104
Views
How can I get value from loop select in template?

I wan to get value of item price in js for every select what would be my jquery function?

                        {% for package in packages %}
                            <div class="form-group col-md-6">
                                <label for="bedrooms">{{ package.title }}<span style="color: red;">*</span></label>
                                <select class="form-control input-dsn" required id="bedrooms" name="package_{{ package.id }}" onChange="document.getElementByName('package_{{ package.id }}').value = this.value;">

                                    {% for item in package.item_set.all %}
                                        <option  value="{{ item.id }}" data-item-value="{{ item.price }}">{{ item.title }}</option>
                                    {% endfor %}

                                </select>
                            </div>
                        {% endfor %}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This will (I think) iterate over all the options calling a function on each one.

$(document).ready(function() {
    $("#bedrooms option").each(function() {
        $(this).attr("data-item-value") // is the value you want    
    });
});

Personally I tend to "cheat" and include small JQuery scriptlets in my template where Django can substitute the values I want. The only thing you have to remember is that certain whitespaces become significant. { { is JS. {{ is going to cause the Django template engine to get indigestion!

var option_values = [
    {% for item in package.item_set.all %}{{ item.price }}, {%endfor%} ];
about 4 years ago · Juan Pablo Isaza Report
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!