...

View Full Version : If key isnt enter



tomharto
09-19-2011, 10:17 PM
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?


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();
}

blaze4218
09-19-2011, 10:27 PM
http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/javascript-char-codes-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

tomharto
09-19-2011, 10:30 PM
So by a (quick) look the keycode is 13 so would it be done like this?




if (this.keyCode !== "13)
{

}
else
{
//do the script
}

blaze4218
09-19-2011, 10:32 PM
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

tomharto
09-19-2011, 10:33 PM
Oops, so i did, thanks. Do you mean you'd use != rather than !==?

blaze4218
09-19-2011, 10:34 PM
yeah, but that's just a preference of mine, pay no attention if you don't want to

tomharto
09-19-2011, 10:34 PM
Ahh okay :)

tomharto
09-19-2011, 10:36 PM
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


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

tomharto
09-19-2011, 10:38 PM
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

blaze4218
09-19-2011, 10:39 PM
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)

blaze4218
09-19-2011, 10:40 PM
lol :)

tomharto
09-19-2011, 10:40 PM
This is what i ended up with


$("#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();
}
});

blaze4218
09-19-2011, 10:41 PM
function displayKeyCode(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
}

blaze4218
09-19-2011, 10:41 PM
I don't speak php... did it run?

blaze4218
09-19-2011, 10:44 PM
wait, is that php? because your earlier post indicated "PHP Code"

tomharto
09-19-2011, 11:12 PM
Nah it is javascript i must have just hit the PHP code button my habit (99% of my posts are in the php section :P)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum