SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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?

    Code JavaScript:
    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;
    }

  2. #2
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    how are you hooking the element that calls said function?

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    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.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try using onkeyup instead as no matter what as soon as the key is released the onkeydown event will have no effect.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  5. #5
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,051
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    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.
    The only code I hate more than my own is everyone else's.

  7. #7
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    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?

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    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.

  9. #9
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by unemployment View Post
    Does having return false in a function do nothing?
    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.

    Quote Originally Posted by unemployment View Post
    I don't just want the firstchild either.
    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...

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •