How can I find ID/Class of the current form within the ajax success function? Since there are multiple forms with the same Class, I cant simply search for the class, but need exactly the form that was submitted.
I have following script:
$('#fAddOs,#fAddFs,.addJ').submit(function(){
//$(this) = form
$.ajax({
type:'POST',
url:'admin.inc.php',
data:data,
dataType:'html',
success:
function(response){
//Get form
}
});
});
If I call $(this)
within the AJAX function (see my comment in the code above), I get:
[Object { url="admin.inc.php", isLocal=false, global=true, mehr...}]
With the selector you're using, you are grabbing multiple elements. Inside the submit, the current form being worked with can be accessed using this
. Or to use it as a jQuery object $(this)
.
In the function:
var id = $(this).attr("id");
Or to grab the class name
var class = $(this).attr("class");
UPDATE
Before the ajax call, create a var:
var form = $(this);
then use form
in the ajax function
outside the ajax call, save the form to a variable that you can use inside the ajax callback.
$('#fAddOs,#fAddFs,.addJ').submit(function(){
var form = $(this);
$.ajax({
type:'POST',
url:'admin.inc.php',
data:data,
dataType:'html',
success:
function(response){
// use the form variable here...
console.log(form.attr('id'));
}
});
});
Try this within the submit function:
var id = this.id;