javascript form won't submit() from escape key captured with addEventListener -


update - added following link html form. when clicked, form.submit() works should.

<a href="#" onclick="post_to_url('abc');">click</a> 

form.submit() doesn't work when capturing keypress.

update 2 - changed event.keycode == 27 event.keycode == 65 , voila, press of 'a' key, submit() work. problem interference escape key. ideas?

update 3 - solved - solution change keydown keyup in code below

end update

my goal capture user escape keypress , go new url. have captured escape key , called function creates form on fly submit passed url. have verified url has been passed correctly alert(), form won't submit. can't figure out isn't working. in advance.

the key handler captures escape key here (note: solution change keydown keyup in code below- left alone integrity of post) :

<script type="text/javascript"> window.addeventlistener("keydown", function(event) {     // bind both command (for mac) , control (for win/linux)     if (event.keycode == 27) {               if (document.getelementbyid('url')){         path = document.getelementbyid('url').value;         post_to_url(path);     }     } }, false); </script> 

the function create form post here:

<script type="text/javascript"> function post_to_url(path, params, method) { method = method || "post"; // set method post default if not specified. // rest of code assumes not using library. // can made less wordy if use one. var form = document.createelement("form"); form.setattribute("method", method); form.setattribute("action", path); for(var key in params) {     if(params.hasownproperty(key)) {         var hiddenfield = document.createelement("input");         hiddenfield.setattribute("type", "hidden");         hiddenfield.setattribute("name", key);         hiddenfield.setattribute("value", params[key]);          form.appendchild(hiddenfield);      } }  document.body.appendchild(form); form.submit(); } </script> 

i'm including html code make example complete:

<body>     <form id = 'close' action='' method="post">         <input id="url" type="text" value="urlhere">         <input type="submit" name="ok">         <!--when clicking on - form.submit() works should -->         <a href="#" onclick="post_to_url('abc');">click</a>     </form> </body> 

first make sure passing params argument when call post_to_url. doesnt seem happen inside keydown event handler
second check value of document.getelementbyid('url').value points script want data submitted to.
third, make sure params argument js object members/keys.

n.b.you not need add form document in order submit.

edit after going thru above checklist, should have following code works.....

var prm = {"abc":"123"}//object submitted  function post_to_url(path, params, method) {     method = method || "post"; // set method post default if not specified.     // rest of code assumes not using library.     // can made less wordy if use one.     var form = document.createelement("form");     form.setattribute("method", method);     form.setattribute("action", path);     for(var key in params)          if(params.hasownproperty(key)) {             var hiddenfield = document.createelement("input");             hiddenfield.setattribute("type", "hidden");             hiddenfield.setattribute("name", key);             hiddenfield.setattribute("value", params[key]);             form.appendchild(hiddenfield);          }     form.submit(); }  window.addeventlistener("keydown", function(event) {     // bind both command (for mac) , control (for win/linux)     if (event.keycode == 27) {                   post_to_url("index.php",prm);//swap index.php url, prm, see above     } }, false); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -