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 %}
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%} ];