I'm trying to create a jQuery class to validate input fields when a form is submitted. I have different forms into the same page and I need to create a validator instance for each form.
HTML
<form method="post" action="index.php" id="form1">
<input type="submit" value="Send" />
</form>
<form method="post" action="index.php" id="form2">
<input type="submit" value="Send" />
</form>
<script type="text/javascript">
var form1 = new DataValidator('#form1');
var form2 = new DataValidator('#form2');
</script>
I would access object variables from submit event handler, but when I print this.selector variable, I see an undefined value.
JavaScript
function DataValidator(selector)
{
this.form = $(selector);
this.selector = selector;
$(selector).submit(function()
{
alert('submit ' + this.selector);
return false;
});
}
thison MDN