I want to empty the listed field value to empty on success. In the following code a url is called and passed value to the page and on that page the values are get and inserted in to mysql, so on succees function I want the field from which the value is get to become empty.
$('#datacouform').click(function() {
var couname = $('#namecoun').attr('value');
var coucode = $('#codecoun').attr('value');
if ((couname === '') || (coucode === '')) {
$('p#errorcou').show(200);
return false;
}
else if ((!couname.match(/^[a-zA-Z]+$/)) || (!coucode.match(/^[a-zA-Z]+$/))) {
$('p#errorcou').hide(20);
$('p#errorcou').show(50);
return false;
} else {
$('a#counloader').show();
$.ajax({
type: 'POST',
url: 'adddata.php',
data: 'counname=' + couname + '&councode=' + coucode,
success: function() {
$('a#counloader').hide();
$('#addcountryform').hide(400);
$('#addcountry').show(400);
}
});
return false;
}
});
Assuming that ‘#namecoun’ and ‘#codecoun’ are just inputs, I made a few tweaks to the code snippet you gave. Try the following:
Instead of using “.attr(‘value’)” to grab the value of the input, I used jQuery .val() which returns the value as a string. I then used “.length” to see whether the values were empty or not. I removed the returns as it’s implied. Using the same .val() function, you can assign values to those inputs by placing a string in the parameters like .val(‘value’). In this case, we want the fields empty so I gave it an empty string .val(”)
Hope that helps.