{"id":10828,"date":"2022-09-25T03:23:12","date_gmt":"2022-09-24T21:53:12","guid":{"rendered":"http:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/"},"modified":"2022-09-25T03:23:12","modified_gmt":"2022-09-24T21:53:12","slug":"solved-i-cant-get-form-submit-to-work-in-html-javascript","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/","title":{"rendered":"[Solved] I can&#8217;t get form submit to work in html \/ JavaScript"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-47943876\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"47943876\" data-parentid=\"47943741\" data-score=\"-1\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You can use the <code>javascript<\/code> methods <code>onchange<\/code> or <code>onkeydown<\/code> to trigger input from the input field, you don&#8217;t need to submit a form. But in case you needed just that I added the example. I used <code>jQuery<\/code> instead of <code>plain javascript<\/code> to write the functions because now they practically become <code>one-line<\/code> functions.<\/p>\n<p><code>onchange<\/code> will wait for the user to press enter or for the input element to loose focus to call the function.<\/p>\n<p><code>onkeydown<\/code> will call the function on every key press.<\/p>\n<p><code>e.preventDefault()<\/code> cancels the default action of the element, which in this case is a submit action, and lets us make the decision through code whether to submit or not.<\/p>\n<p>Below are some <code>javascript\/jQuery<\/code> test functions and a sample <code>HTML<\/code> file so you can test out what works best for you.<\/p>\n<p><strong>EDIT:<\/strong> I added some examples on how to store the current value of an input  field into a variable<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>\/\/ get the Value of input element directly into a variable\r\nvar myVariable = $('#theInput_1').val();\r\n\r\n\/\/ myVariable will return empty string since the input element is empty\r\nconsole.log('This is the starting value of the 1st input element: ' + myVariable);\r\n\r\n\r\n\/\/ Function for onkeydown test\r\nfunction testKeyDown()\r\n{\r\n  \/\/ stored in a variable which is not available outside the function\r\n  var myVariable = $('#theInput_1').val();\r\n  $('#paraOne').text(myVariable);\r\n  \r\n  \/\/ test output - $('#theInput_1').val() will return empty\r\nconsole.log('This is the changed value of the 1st input element: ' + myVariable);\r\n}\r\n\r\n\/\/ Function for onchange test\r\nfunction testOnChange()\r\n{\r\n  $('#paraTwo').text($('#theInput_2').val());\r\n}\r\n\r\n\/\/ Function for submit test\r\n$( \"#submit\" ).on( \"click\", function(e)\r\n{\r\n  e.preventDefault(); \/\/ Prevents default action of submit\r\n  $('#paraThree').text($('#theInput_3').val());\r\n});<\/code><\/pre>\n<pre class=\"snippet-code-html lang-html prettyprint-override\"><code>&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;Title of the document&lt;\/title&gt;\r\n&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.1.1\/jquery.min.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;p id=\"paraOne\"&gt;This text will be replaced on success.&lt;\/p&gt;\r\n&lt;input type=\"text\" id=\"theInput_1\" onkeydown=\"testKeyDown();\" size=\"50\" value=\"\" placeholder=\"onkeydown test\" \/&gt;\r\n\r\n&lt;p id=\"paraTwo\"&gt;This text will be replaced on success.&lt;\/p&gt;\r\n&lt;input type=\"text\" id=\"theInput_2\" onchange=\"testOnChange();\" size=\"50\" value=\"\" placeholder=\"onchange test\" \/&gt;\r\n\r\n&lt;p id=\"paraThree\"&gt;This text will be replaced on success.&lt;\/p&gt;\r\n&lt;form&gt;\r\n&lt;input type=\"text\" id=\"theInput_3\" size=\"50\" value=\"\" placeholder=\"form submit test\" \/&gt;\r\n&lt;input type=\"submit\" id=\"submit\" value=\"Submit me\" \/&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<\/div>\n<\/div><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved I can&#8217;t get form submit to work in html \/ JavaScript <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You can use the javascript methods onchange or onkeydown to trigger input from the input field, you don&#8217;t need to submit a form. But in case you needed just that I added the example. I used jQuery instead of plain javascript to write the functions because now they practically become one-line functions. onchange will &#8230; <a title=\"[Solved] I can&#8217;t get form submit to work in html \/ JavaScript\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/\" aria-label=\"More on [Solved] I can&#8217;t get form submit to work in html \/ JavaScript\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[346,333,2968,1080],"class_list":["post-10828","post","type-post","status-publish","format-standard","hentry","category-solved","tag-html","tag-javascript","tag-submit","tag-text"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] I can&#039;t get form submit to work in html \/ JavaScript - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] I can&#039;t get form submit to work in html \/ JavaScript - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You can use the javascript methods onchange or onkeydown to trigger input from the input field, you don&#8217;t need to submit a form. But in case you needed just that I added the example. I used jQuery instead of plain javascript to write the functions because now they practically become one-line functions. onchange will ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-24T21:53:12+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] I can&#8217;t get form submit to work in html \/ JavaScript\",\"datePublished\":\"2022-09-24T21:53:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/\"},\"wordCount\":166,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"html\",\"javascript\",\"submit\",\"text\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/\",\"name\":\"[Solved] I can't get form submit to work in html \/ JavaScript - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-24T21:53:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] I can&#8217;t get form submit to work in html \/ JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] I can't get form submit to work in html \/ JavaScript - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] I can't get form submit to work in html \/ JavaScript - JassWeb","og_description":"[ad_1] You can use the javascript methods onchange or onkeydown to trigger input from the input field, you don&#8217;t need to submit a form. But in case you needed just that I added the example. I used jQuery instead of plain javascript to write the functions because now they practically become one-line functions. onchange will ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/","og_site_name":"JassWeb","article_published_time":"2022-09-24T21:53:12+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] I can&#8217;t get form submit to work in html \/ JavaScript","datePublished":"2022-09-24T21:53:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/"},"wordCount":166,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["html","javascript","submit","text"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/","url":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/","name":"[Solved] I can't get form submit to work in html \/ JavaScript - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-24T21:53:12+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-i-cant-get-form-submit-to-work-in-html-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] I can&#8217;t get form submit to work in html \/ JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=10828"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10828\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=10828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=10828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=10828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}