pass variables in functions
Hi
I have this code, but I want to put it in functions and I don't know how to pass the variables.
This is the code:
[code]
<script type="text/javascript">
<!--
$(document).ready(function () {
// Username validation logic
var validateUsername = $('#validateUsername');
var validateTitle = $('#validateTitle');
$('#naam').focus(function() {
$(this).addClass('with-box');
validateUsername.css('display','block');
validateUsername.addClass('info');
});
$('#title').focus(function(){
$(this).addClass('with-box');
validateTitle.css('display','block');
validateTitle.addClass('info');
});
$('#naam').keyup(function () {
// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
var t = this;
// only run the check if the username has actually changed - also means we skip meta keys (= ctrl, ... )
if (this.value != this.lastValue) {
// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
// a particular key, it will only fire when the release the key.
if (this.timer) clearTimeout(this.timer);
// show our holding text in the validation message space
validateUsername.html('<img src="images/ajax-loader.gif" height="12" width="12" /> checking availability...');
removeClassUsername();
validateUsername.addClass('info');
// fire an ajax request in 1/5 of a second
this.timer = setTimeout(function () {
$.ajax({
//naam van huidige bestand
url: 'submit.php',
data: 'action=check_username&naam=' + t.value,
dataType: 'json',
type: 'post',
success: function (j) {
// put the 'msg' field from the $resp array from check_username (php code) in to the validation message
validateUsername.html(j.msg);
/*
$('#naam').focusout(function(){
$(this).removeClass('with-box');
$('#validateUsername').css('display','none');
});
*/
if(j.state == 'info')
{
removeClassUsername();
validateUsername.addClass('info');
}
else if (j.state == 'good')
{
removeClassUsername();
validateUsername.addClass('good');
}
else if (j.state == 'error')
{
removeClassUsername();
validateUsername.addClass('error');
}
}
});
}, 200);
// copy the latest value to avoid sending requests when we don't need to
this.lastValue = this.value;
}
});
$('#title').keyup(function () {
// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
var t = this;
// only run the check if the username has actually changed - also means we skip meta keys (= ctrl, ... )
if (this.value != this.lastValue) {
// the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
// a particular key, it will only fire when the release the key.
if (this.timer) clearTimeout(this.timer);
// fire an ajax request in 1/5 of a second
this.timer = setTimeout(function () {
$.ajax({
//naam van huidige bestand
url: 'submit.php',
data: 'action=check_title&title=' + t.value,
dataType: 'json',
type: 'post',
success: function (j) {
// put the 'msg' field from the $resp array from check_username (php code) in to the validation message
validateTitle.html(j.msg);
/*
$('#naam').focusout(function(){
$(this).removeClass('with-box');
$('#validateUsername').css('display','none');
});
*/
if(j.state == 'info')
{
removeClassTitle();
validateTitle.addClass('info');
}
else if (j.state == 'good')
{
removeClassTitle();
validateTitle.addClass('good');
}
else if (j.state == 'error')
{
removeClassTitle();
validateTitle.addClass('error');
}
}
});
}, 200);
// copy the latest value to avoid sending requests when we don't need to
this.lastValue = this.value;
}
});
//$(window).load(function () { checkValid(); } );
function removeClassUsername()
{
validateUsername.removeClass('info').removeClass('error').removeClass('good');
};
function removeClassTitle()
{
validateTitle.removeClass('info').removeClass('error').removeClass('good');
};
});
</script>
[/code]
I need 2 functions. A 'keyup' function so I just have to pass the input that is active.
And a removeClass function to remove the current classes.
If you don't understand the code, doesn't matter I just need the functions and pass the variables correct.
Thanks!