jQuery reference to (this) does not work?
- by FFish
I have this href link with text either "attivo" or "non attivo"
User can set the item to 'active' or 'closed' in the database with an ajax request $.post()
I have 2 questions for these:
I can't get the reference to $(this) to work.. I tried it with a normal link and it works, but not wrapped in if/else??
How can I prevent the user from clicking more than one time on the link and submitting several request? Is this a valid concern? Do I need some sort of a small timer or something?
First I was thinking about a javascript confirm message, but that's pretty annoying for this function..
HTML:
<dl id='album-list'>
<dt id="dt-2">some title</dt>
<dd id="dd-2">
some description<br />
<div class='links-right'>status: <a class='toggle-active' href='#'>non attivo</a></div>
</dd>
</dl>
<a class="test" href="#">test</a>
JS:
$('dd a.toggle-active').click(function() {
var a_ref = $(this);
var id = a_ref.parent().parent().attr('id').substring(3);
if (a_ref.text() == "non attivo") {
var new_active = "active"; // for db in english
$.post("ajax-aa.php", {album_id:id, album_active:new_active},
function(data) {
// alert("success");
a_ref.text("non attivo"); // change href text
});
} else {
var new_active = "closed"; // for db in english
$.post("ajax-aa.php", {album_id:id, album_active:new_active},
function(data) {
// alert("success");
a_ref.text("attivo"); // change href text
});
}
return false;
});
$('a.test').click(function() {
var a_ref = $(this);
$.post("ajax-aa.php", {album_id:2, album_active:"active"},
function(data) {
a_ref.text("changed");
});
return false;
})