Why won't return false disable enter?

I am trying to disable the user from being able to go to a new line in my text area. Why doesn’t return false work?

if (is_key(event, 13) && (with_field.value.length > 1)){					
	var search_list_ele_array = new Array();
	var search_list_ele_array = document.getElementById('with_auto').getElementsByTagName('li');

	for (j=0; j<search_list_ele_array.length; j++) {
		if ((search_list_ele_array[j].className == 'user list_hover') || (search_list_ele_array[j].className == 'comp list_hover') || (search_list_ele_array[j].className == 'blog list_hover')){
			var link = search_list_ele_array[j].getElementsByTagName('a');
			user_add(link[0], search);
	return false;

how are you hooking the element that calls said function?

I don’t know exactly what you mean. I am using an onkeydown function call.

Try using onkeyup instead as no matter what as soon as the key is released the onkeydown event will have no effect.

I’d be thinking you’d need to be trapping onkeypress… though you may have to trap all three.

… and maybe onsubmit if that’s inside a form. Sometimes ‘command’ keys like enter aren’t passed through the keyup/keydown properly.

Is that the complete code? If it is you have a ways to go before your able to achieve your goals. return in a none function context… where did you pick that up.

Oh, side note – the empty array declaration before the getElements is pointless, and since all those if statements do the same thing off the same variable, you should be using case with drop-through instead of IF.

Also, I’d have to see the HTML this is being run upon, but if that anchor you’re looking for is the first child element of the LI, it may be more effective to do ‘firstChild’ instead of the rather slow (painfully slow in legacy browsers with shiv’s) getElementsByTagName.

Oh, and is it supposed to return null if enter is hit but the field is empty?

Hmm ok well I supposed I can remove the empty array declaration. Does having return false in a function do nothing? I don’t just want the firstchild either.

That should work – you’re just trapping the keyboard wrong and/or not enough. again, onkeyup and/or onkeypress and/or onsubmit if inside a form. onkeydown alone? Unlikely to work.

since you’re only pulling link[0] I’d assume there’s a fixed relationship.

But again, without seeing the FULL code, as in the HTML that’s being processed, the FULL scripting (lack of which is confusing Oddz and likely everyone else) including the METHOD of hooking the element you’re processing (are you adding it with javascript, are you doing it in the markup) … well… I have a saying, CSS without the HTML it’s applied to is gibberish; In this case javascript without the HTML it’s applied to is, well…

Good thing to know… Well I used onkeypress and that seemed to work just fine. :slight_smile: