#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    36
    Rep Power
    6

    JQuery equivalent "on (release, keyPress "<Enter>") {"


    Hi.

    i want to send some variable with jQuery but i have only a script for "onclick" function. i want also to send the variable on keypress "enter". equivalent of flash actionscript code: "on (release, keyPress "<Enter>") {"

    this is my script:

    Code:
    $(function() {
    
    $("#sendmail").click(function() {
    
    var email = $("#email").val();
    
    var dataString = 'email='+email;
    
    if(email != '') {
    
    $.ajax({
    
    type: "POST",
    
    url: "sendmail.asp",
    
    data: dataString,
    
    cache: false,
    
    success: function(html){
    
    $("#email").val("");
    
    }
    
    });
    
    } return false;
    
    }
    
    });
    
    });
    
    <input id="email" type="text"/><input value="Submit" id="sendmail" type="submit"/>
    how i can send the variable onclick or onEnter?

    thanks in advance
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Hi,

    do you wanna submit the form if the user presses enter while the cursor is in the text field? That's the default behaviour, anyway. All you need to do is write proper HTML (with a form and stuff):

    Code:
    <form action="sendmail.asp">
    	<fieldset>
    		<input id="email" type="text"/><input value="Submit" id="sendmail" type="submit"/>
    	</fieldset>
    </form>
    Or do you want the enter key to trigger the submission in any case? That's a bad idea, because it's not what the user expects.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    36
    Rep Power
    6
    Originally Posted by Jacques1
    Hi,

    do you wanna submit the form if the user presses enter while the cursor is in the text field? That's the default behaviour, anyway. All you need to do is write proper HTML (with a form and stuff):

    Code:
    <form action="sendmail.asp">
    	<fieldset>
    		<input id="email" type="text"/><input value="Submit" id="sendmail" type="submit"/>
    	</fieldset>
    </form>
    Or do you want the enter key to trigger the submission in any case? That's a bad idea, because it's not what the user expects.
    i don't want to reload the page. i want to send via jQuery (xmlhttprequest) the text inside of the textinput.
    i don't want the form behavior because of page reload like i said.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Originally Posted by Kyriakos
    i don't want the form behavior because of page reload like i said.
    Have you actually tried the new form?

    The form doesn't reload anything as long as you have that return false; in your code. The only difference is that your AJAX stuff will be triggered both by clicking on the submit button and pressing the enter key while the cursor is in the text field. I thought that's what you wanted?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    36
    Rep Power
    6
    Originally Posted by Jacques1
    Have you actually tried the new form?

    The form doesn't reload anything as long as you have that return false; in your code. The only difference is that your AJAX stuff will be triggered both by clicking on the submit button and pressing the enter key while the cursor is in the text field. I thought that's what you wanted?
    i tried your way and it's working. thank you my friend.
  10. #6
  11. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,122
    Rep Power
    4258
    Also it's better to use the form element's submit event instead of the submit button's click event.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo