How to tab through form elements using the enter key in Jquery
===

I know there are a couple of entries already dealing with this sort of problem and I have looked at quite a few, but none of then seem to work in my situation. Any help with this will be greatly appreciated.

What I have is a dynamically generated form (there can be a varied amount of inputs). I need to tab through them using my enter key. Currently the tab key tabs trough the elements fine, but my criteria requires me to enable the enter key. When I reach the final input and I press the enter, it needs execute the submit action that is currently done via an a tag 

Below are code snippets of my HTML form as well as my JS that handles the keypress event

    <div class="credential_form">
	<div class="error_msg">
		<h4>There was a problem connecting.</h4>
	</div>
	<div class="credential_inputs">
		<div class="txt_wrapper">
			<input type="text" style="width: 130px">
		</div>
		<div class="txt_wrapper">
			<input type="password" style="width: 130px; display: none;" class="real">
		</div>
		<div class="clear"></div>
	</div>
	<div class="add_button">
		<a class="button" href="javascript:void(0)"><span>Add</span></a>
	</div>
	<div class="clear"></div>
	<div class="sub_input">
		<div class="link">
			<span class="sub_text">Forget your details? Go to&nbsp;</span>
			<a class="sub_link" href="http:somewhere.com">Somewhere</a>
		</div>
	</div>
    </div>

For some reason my collection variable does not recognize the current element in the if conditional.

    var code = (event.keyCode ? event.keyCode : event.which);

    if (code == 13) {

	var $inputs = $(event.target).closest('.credential_inputs').children($(':input:visible'));
	var $addButton = $(event.target).closest('.credential_form').children($('a:first-child'));
	
	if ( $(event.target) == $inputs[length - 1] ) {
		$addButton.click()
	} else {
		$(event.target).closest(".credential_inputs").nextAll('input:first').focus();
	}
    }

Thanks