Javascript function on pressing (enter) key if field equals

I need to make an enter key so that the user will use a different url based on the value in a specific field.
This is what I still have   

 <script type="text/javascript">  document.onkeyup = KeyPressed;   
     function KeyPressed( e )  {    
         var key = ( window.event ) ? event.keyCode : e.keyCode;         
         switch( key )    {      
             case 13:        
                 if (this.getField("ls").value == 'x') {
                     alert ("its x")
                     window.location="ccamntdue.html"

                 } else if (this.getField("ls").value == ' ') {
                     alert ("its not")


                 } else {



                 }   

</script>

      

I can remove the argument and get enterkey to return a warning for testing to make sure it sees the enter key. The problem seems to be in the argument. I've included a few warnings to check if any part of the argument works, but cannont seems to make it work. It's been a few years since I've been working with html / Javascript and I'm a little rusty.
I would like this to check if field = "a" then take user to "A_url.html", if field is "b" then take user to "b_url.html" etc. After pressing the enter key.

+3


source to share


2 answers


I think you are using DynaForms Objects ...

Try as below ... it will help you ...



<body onkeyup="KeyPressed(event)">

<script>
function KeyPressed(event)
{
switch(event.keyCode)
{
case 13:
    switch (getField("ls").value)
    {
     case "a":
     window.location.assign("https://www.google.com");
     break;
     case "b":
     window.location.assign("http://www.bing.com/");
     break;
     default:
     //Default Statement;
    }
break;
default:
//Default Statement;
}
}
</script>

      

+1


source


Here's something to do it. Assuming you provided your input id ls

http://jsfiddle.net/GZNYc/

document.onkeyup = function ( e )  {
    var key = ( window.event ) ? event.keyCode : e.keyCode;
    if (key == 13) {
        var value = document.getElementById('ls').value;
        if (value === 'x') {
            window.location = "x.html";            
        } else {
            window.location = "a.html";
        }    
    }
};

      



The main problem with your code is that document

there is no method on the object getField

(which is pointed out this

in your handler). Its syntax is also invalid, but that doesn't sound like your question.

-1


source







All Articles