Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  63] [ 1]  / answers: 1 / hits: 17489  / 14 Years ago, fri, may 28, 2010, 12:00:00

I am currently trying to hook up jQuery UI dialog so that I may use it to create new items to my page and to modify ones existing already on the page. I managed in the former. I'm currently struggling in the latter problem, however. I just cannot find a nice way to pass the item to modify to the dialog.



Here's some code to illustrate the issue better. Note especially the part marked with XXX. The {{}} parts are derived from Django template syntax:



$(.exercise).click(function() {
$.post({{ request.path }}, {
action: create_dialog,
exercise_name: $(this).text()
},
function(data) {
$(#modify_exercise).html(data.content);
},
json
);

$(#modify_exercise).dialog('open');
});

$(#modify_exercise).dialog({
autoOpen: false,
resizable: false,
modal: true,
buttons: {
'{% trans 'Modify' %}': function() {
var $inputs = $('#modify_exercise :input');

var post_values = {};
$inputs.each(function() {
post_values[this.name] = $(this).val();
});

post_values.action = 'validate_form';

//XXX: how to get the exercise name here?
post_values.exercise_name = 'foobar';

$.post('{{ request.path }}', post_values,
function(data) {
if( data.status == 'invalid' ) {
$('#modify_exercise').html(data.content);
}
else {
location.reload();
}
},
json
);
}
}
});


Here's some markup to show how the code relates to the structure:



<div id=modify_exercise class=dialog title={% trans 'Modify exercise' %}>
</div>

<ul>
{% for exercise in exercises %}
<li>
<a class=exercise href=# title={{ exercise.description }}>
{{ exercise.name }}
</a>
</li>
{% endfor %}
</ul>

More From » jquery

 Answers
22

if you're working with eventhandlers you might want to use the event object instead of some global var ;)



event.target is what you're looking for.



e.g.



$('.sel').bind('dialogcreate', function(event, ui) {
event.target.innerHTML = 'new content';
});

[#96648] Wednesday, May 26, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorw

Total Points: 484
Total Questions: 120
Total Answers: 107

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;