Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    If key isnt enter

    I have this which runs on keyup in a textbox but i dont want it to run if the pressed key is enter, how would i go about that?

    Code:
    function getList()
    {
    	var xmlhttp;
    	if (window.XMLHttpRequest)
    	{// code for IE7+, Firefox, Chrome, Opera, Safari
    		xmlhttp=new XMLHttpRequest();
    	}
    	else
    	{// code for IE6, IE5
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function()
    	{
    		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			showDiv('#', 'result');
    			document.getElementById("result").innerHTML=xmlhttp.responseText;
    		}
    	}
    	var user = document.getElementById('friendsSearch').value;
    	xmlhttp.open("GET","../friendSearch.php?username="+user,true);
    	xmlhttp.send();	
    }

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    http://www.cambiaresearch.com/c4/702...key-codes.aspx
    Browser Dependency
    Jonathan Tang 19 Oct 08, 13:57

    Key codes are heavily dependent upon browsers. There's a full table available at:

    http://unixpapa.com/js/key.html

    There's also a library that normalizes keycodes into a consistent set, available at:

    http://jonathan.tang.name/code/js_keycode

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    So by a (quick) look the keycode is 13 so would it be done like this?

    Code:
    if (this.keyCode !== "13)
    {
    
    }
    else
    {
    //do the script
    }

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    yep, I don't usually use the strict !== unless it is specifically necessary, but no harm, also, you missed an end parenthesis. end quotation, sorry

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Oops, so i did, thanks. Do you mean you'd use != rather than !==?

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    yeah, but that's just a preference of mine, pay no attention if you don't want to

  • #7
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Ahh okay

  • #8
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    It didnt seem to work so i put in alert(this.keyCode); and it displays undefined. Any idea why? youve got the JS function up above and its called by this

    PHP Code:
    <input id="friendsSearch" name="username" type='text' onkeyup="getList()" style="color:#777;margin-left:245px;" autocomplete="off" maxlength="15" 

  • #9
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    EDIT: Duh -.-, i just remembered i have a function running if the pressed key IS enter, ill just put the getList function inside the else there.

    Thats what you get when you drink and code :P

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    oh, the "this" is would work only if you had first created a function to handle the "event"
    sorry, check the source on that site, there is a function set up to do that (do a quick search for the term 'which', that should take you right to the function)

  • #11
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    lol

  • #12
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    This is what i ended up with

    Code:
    $("#friendsSearch").keyup(function(event)
    	{
     		if(event.keyCode == 13)
    		{
    			$("#result").slideUp();
    			$("#result").hide();
    			$("#friendResult").html("Processing&hellip;");
    			$("#friendResult").fadeIn();
    			
    			var name = document.getElementById('friendsSearch').value;
    			$.ajax({
    				type: "POST",
    				url: "pages/viewProfile.php",
    				data: "name="+name+"",
    				success: function(msg){
    					$("#friendResult").html(msg);
    				}
    			});
    		}
    		else
    		{
    			var xmlhttp;
    	if (window.XMLHttpRequest)
    	{// code for IE7+, Firefox, Chrome, Opera, Safari
    		xmlhttp=new XMLHttpRequest();
    	}
    	else
    	{// code for IE6, IE5
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function()
    	{
    		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			showDiv('#', 'result');
    			document.getElementById("result").innerHTML=xmlhttp.responseText;
    		}
    	}
    	var user = document.getElementById('friendsSearch').value;
    	xmlhttp.open("GET","../friendSearch.php?username="+user,true);
    	xmlhttp.send();	
    		}
    	});

  • #13
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    function displayKeyCode(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    }

  • #14
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I don't speak php... did it run?

  • #15
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    wait, is that php? because your earlier post indicated "PHP Code"


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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