0

I have a issue to capture the class for my address field. I want to stop the enter key (char[13]) from submitting the form, which I seemed to have done. But as a exception for my text blocks, in this case a address field, I want to allow next line.

The problem is that if I run this code, myClass in the console is said to be undefined. If I can capture the class name then I can perform the next step which is to allow "next line" for that object.

Am I going down the right track? In which case can someone please guide me. Or if the solution should be completely different, please advise me how.

$(window).keypress(function(event){
    var myClass = item.attr('class');
    if(myClass == 'addressClass'){
        if(keycode == '13') {
            /* Code for NextLine */
            event.preventDefault();
            console.debug('New Line and Prevent Submit.');
        }
        console.debug(myClass);
    }

    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13') {
        event.preventDefault();
        console.debug('Prevent Submit.');
    }
});
morne
  • 4,035
  • 9
  • 50
  • 96

1 Answers1

1

item.attr('class') is undefined cause "item" is undefined. Instead of attr() try to use

if (item.hasClass('addressClass'))

and before that try to define

var item = $(event.target);

or bind keypress event to text inputs

Silwerclaw
  • 685
  • 5
  • 13
  • Thanks. Worked nicely. Sorry but Im a bit new to JQuery and did not note the item not being defined. – morne Sep 05 '13 at 10:22