Trouble using 'return' charachter as a trigger for ajax request

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bugboy
    New Member
    • Sep 2007
    • 160

    Trouble using 'return' charachter as a trigger for ajax request

    I need to change the content of a div using ajax when i hit the 'return' key on my keyboard. How do i get js to aknowledge the 'return' character? chr(13) doesn't seem to work.
    Thanks!

    Code:
    function handle_textarea(t, n)
    	{ 
    	var val=t.value;
    	var the_length=value.length;
    	var last_char=val.charAt(the_length-1);
    
    	if (last_char=='chr(13)')
    		{
    		xmlHttp=GetXmlHttpObject()
    		if (xmlHttp==null)
    			 {
    			 alert ("Browser does not support HTTP Request")
    			 return
    			 }
    		var url="new_row.php"
    		url=url+"?row_num="+row_num
    		url=url+"&rand="+Math.random()
    		xmlHttp.onreadystatechange=list_stateChange('list_id')
    		xmlHttp.open("GET",url,true)
    		xmlHttp.send(null)
    		}
    	else
    		{
    		resize_textarea(t, n);		
    		}
    	}
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    Code:
    function el(tid) {return document.getElementById(tid);}
    
    	el("textAreaID").onkeydown=function(e){ 
    		e=e||window.event;
    		var kc = e.keyCode; 
    		if(kc==13){
    			alert("Enter key pressed");
    		}
    	}

    Comment

    • bugboy
      New Member
      • Sep 2007
      • 160

      #3
      Ok so i put the function in my include file and the function call into script tags in my page and now i have another problem. There is another onkeydown event that no longer works... oh.. maybe i'll try including it in the same call but as an else.. since if return is hit it doesn't need to work.. else it does..

      Comment

      • bugboy
        New Member
        • Sep 2007
        • 160

        #4
        Ok Cool, it works! Except that i only want the event to trigger if i hit return as the last key stroke in the textarea value. In other words if someone hits return in the middle of an existing sentence in the textarea then i don't want it to trigger.

        This is what i'm trying but i can't quite figure out what's wrong with it.. any additional help would be really appreciated!

        Code:
        		el(\'textarea_id\').onkeydown=function(e)
        		{ 
        		e=e||window.event;
        		var kc = e.keyCode; 
        		var val=document.getElementById(\'textarea_id\').value;
        		var the_length=value.length;
        		var last_char=val.charAt(the_length-1);
        		if(kc==13 && kc==last_char)
        			{
        			new_row(\'1\')
        			}
        			else
        			{
        			resize_textarea(this, \'0\')
        			}
        		}
        Last edited by bugboy; Nov 7 '08, 05:56 PM. Reason: your system always removes the last code tag on me when i publish so i have to go back and add it as an edit

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          yeah, that's gonna severely complicate things.

          in firefox, you can throw the code in an if statement:

          where txt is the textarea object:

          if(txt.value.le ngth == txt.selectionSt art){ //do the submit stuff here }

          for others and IE, it a bit more than that.

          Comment

          • bugboy
            New Member
            • Sep 2007
            • 160

            #6
            Thanks for the help :)

            Comment

            Working...