Reputation: 361
I need a solution to this problem:
I have a django site that displays all recipes in a recipe list
each of these recipes is a link that when clicked should load the fields of that recipe into a popupdiv
so far I have the popup div all set - now i just need to figure out how to access the recipe fields of that button.
basically the content i would like to load is recipe.name recipe.author recipe.steps etc but i don't know how to.
Right now I also have a form being loaded into the div using ajax so I don't want to interfere in anyway with the ajax call (i will post this code as well). Would it make sense to also use ajax for a problem like this?
here is my button click function
$(document).ready(function(){
$(".button").click(function(){
var content = $("#popupContact").load($('#recipe'));
});
});
here is the template for my page:
{% block content %}
{% autopaginate recipe_list 6 %}
<div id="recipe_cont">
{% for recipe in recipe_list %}
<div class="recipe">
<div class="button">//here is the button that is clicked to load recipe
<a href="{% url cookbook.views.userrecipe recipe.id %}" style="display: none;"></a>
<img src="{{ STATIC_URL }}chicknbraw.jpg" alt="" height="70" width="70" style="display:inline;" />
<h4>{{ recipe.name }}</h4>
</div>
<h5>{{ recipe.author }}</h5>
<h5>Prep Time: {{ recipe.prep_time }} minutes</h5>
<h6><a href="/addrecipe/{{ recipe.id }}">Add Recipe</a>
<a href="/removerecipe/{{ recipe.id }}">Remove Recipe</a></h6>
</div>
{% endfor %}
</div>
<div id="popupContact" class="popup">//load recipe information into this div
<a id="popupContactClose" style="cursor:pointer;float:right;">x</a>
<p id="contactArea">
<h1 style="text-align:center">Create New Recipe</h1>
{% include 'cookbook/create_form.html' %} //this is for the form that is loaded. Is there a way to include a different template? Maybe have multiple contactAreas and hide/show different ones depending on what is being loaded?
</p>
</div>
<div id="backgroundPopup">
</div>
<div id="col2-footer">
{% paginate %}
<p id="recipe_order_text"> order by: <a href="/userbook/ordered/name">abc</a>|<a href="/userbook/ordered/date">date</a>
</div>
{% endblock %}
here is the form ajax call:
$(document).ready(function(){
function hijack() {
var form = $('form#createrecipeform');
form.each (function(){
this.reset();
});
form.submit(function(e) {
e.preventDefault();
console.log('ajax form submission function called successfully.');
//form = $(this);
console.log(form)
var serialized_form = form.serialize();
$.ajax({ type: "POST",
url: $(this).attr('action'),
data: serialized_form,
success: (function(data) {
console.log('ajax success function called successfully.');
data = $.parseJSON(data);
if (data.success) {
console.log('success');
} else {
console.log('failure');
var newForm = data.form;
form.replaceWith(newForm);
hijack();
}
})
});
return false;
});
};
hijack();
});
thanks a lot for any solutions you may have
katie
Upvotes: 1
Views: 1417
Reputation: 361
I ended up creating a div that is hidden for each recipe that is loaded and then used .html() to place each of the divs wihtin the popup
this ended up working perfectly - better then replaceWith append or clone
thank you for your help @akhaku
Upvotes: 0
Reputation: 1157
A hack-y way to do it (not recommended!) would be something like the following (in javascript)
$('.button').click(function() {
var recipe = $(this).parent();
var name = $('h4', recipe);
var author = $('h5', recipe);
// etc etc
});
To make this a little bit better, assign classes to the elements containing the name and author, then you can do the following instead of getting the h4 and h5
var name = $('.recipe-name', recipe);
Again this is very much a hack, and should be done differently...
Upvotes: 1