{"id":163,"date":"2021-12-21T15:27:49","date_gmt":"2021-12-21T15:27:49","guid":{"rendered":"https:\/\/kindsonthegenius.com\/elm\/?p=163"},"modified":"2022-01-17T07:21:49","modified_gmt":"2022-01-17T07:21:49","slug":"elm-working-with-button-and-textboxes","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/","title":{"rendered":"Elm &#8211; Working With Button and TextBoxes"},"content":{"rendered":"<p>This tutorial will follow from the previous tutorial where actually rendered the model in the UI.<\/p>\n<p>Now we would be adding a TextBox and a button.<\/p>\n<ol>\n<li><a href=\"#t1\">Add TextBox and a Button<\/a><\/li>\n<li><a href=\"#t2\">Add a ClickHandler to the Button<\/a><\/li>\n<li><a href=\"#t3\">Logging Some Text to the Console<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Add TextBox and a Button<\/strong><\/h4>\n<p>Currently, our view function displays only a text which is the value coming from the model.<\/p>\n<p>Now we want the view to display a TextBox as well as a button. To achieve that you need to modify the view function like so.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0066bb; font-weight: bold;\">view<\/span> model <span style=\"color: #000000; font-weight: bold;\">=<\/span>  \r\n    div <span style=\"color: #333399; font-weight: bold;\">[]<\/span> [\r\n        text(fromInt model<span style=\"color: #333333;\">.<\/span>value),\r\n        div <span style=\"color: #333399; font-weight: bold;\">[]<\/span> <span style=\"color: #333399; font-weight: bold;\">[]<\/span>,\r\n        input <span style=\"color: #333399; font-weight: bold;\">[]<\/span> <span style=\"color: #333399; font-weight: bold;\">[]<\/span>,\r\n        button <span style=\"color: #333399; font-weight: bold;\">[]<\/span> [text <span style=\"background-color: #fff0f0;\">\"Add\"<\/span>]\r\n    ]\r\n<\/pre>\n<p>Also note that I have added an empty div just after the number displayed.<\/p>\n<p><strong>Note you need to do the following<\/strong>:<\/p>\n<ul>\n<li>Add input and button to the import Html statement<\/li>\n<li>Import String exposing fromInt function<\/li>\n<\/ul>\n<p>You can watch the video for the step by step<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Add a ClickHandler to the Button<\/strong><\/h4>\n<p>To achieve this, we first need to take four steps:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Import onClick() from Html.Events. The import below does it:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">Html.Events<\/span> exposing (onClick)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong> &#8211; Add OnClick as first parameter to the button like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0066bb; font-weight: bold;\">button<\/span> [onClick <span style=\"color: #333399; font-weight: bold;\">Add<\/span>] [text <span style=\"background-color: #fff0f0;\">\"Add\"<\/span>]\r\n<\/pre>\n<p>The onClick expects a parameter which is of type message. Here we call the message Add.<\/p>\n<p><strong>Step 3<\/strong> &#8211; Create a message. A message is anything that can be done that affects the model. So we create a message just after the add function like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">type<\/span> <span style=\"color: #333399; font-weight: bold;\">Messages<\/span> <span style=\"color: #000000; font-weight: bold;\">=<\/span>\r\n    <span style=\"color: #333399; font-weight: bold;\">Add<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Since the click would modify the model we must give the update function the message as a first parameter. Then it returns the updated model. For now, we just return the same model. So modify the update function like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0066bb; font-weight: bold;\">update<\/span> msg model <span style=\"color: #000000; font-weight: bold;\">=<\/span>  \r\n    <span style=\"color: #008800; font-weight: bold;\">case<\/span> msg <span style=\"color: #008800; font-weight: bold;\">of<\/span>\r\n        <span style=\"color: #333399; font-weight: bold;\">Add<\/span> <span style=\"color: #000000; font-weight: bold;\">-&gt;<\/span>\r\n            model\r\n<\/pre>\n<p>Cool! Let&#8217;s now talk about logging.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Logging Messages to the Console<\/strong><\/h4>\n<p>Logging some text to the console is in Elm is a bit different that how it is done in JavaScript or Angular.<\/p>\n<p>In Elm, logging is kind of considered a side effect, and so in pure functional language like Elm, it is not normally\u00a0 welcome. However, it could be necessary at time.<\/p>\n<p>Let modify our code to log some text to the console when a button is clicked. Follow the steps:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Import Debug like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">Debug<\/span> exposing (log)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong> &#8211; In the updated function, write the code to log some message enclosed in a let &#8230; in block. This is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0066bb; font-weight: bold;\">update<\/span> msg model <span style=\"color: #000000; font-weight: bold;\">=<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">let<\/span>\r\n        logmessage <span style=\"color: #000000; font-weight: bold;\">=<\/span> log <span style=\"background-color: #fff0f0;\">\"here\"<\/span> <span style=\"background-color: #fff0f0;\">\"Button Clicked\"<\/span>\r\n        logmessage2 <span style=\"color: #000000; font-weight: bold;\">=<\/span> log <span style=\"background-color: #fff0f0;\">\"model\"<\/span> model\r\n    <span style=\"color: #008800; font-weight: bold;\">in<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">case<\/span> msg <span style=\"color: #008800; font-weight: bold;\">of<\/span>\r\n        <span style=\"color: #333399; font-weight: bold;\">Add<\/span> <span style=\"color: #000000; font-weight: bold;\">-&gt;<\/span>\r\n            model\r\n<\/pre>\n<p>What happens here is that when the button is clicked, the update function is called with the parameters: msg and model. Then the content of the <em>let &#8230; in<\/em> block is executed.<\/p>\n<p>Now go ahead to recompile the code and test it out in the browser with the console window open<\/p>\n<p>You will notice that the text &#8220;Button Clicked&#8221; is displayed. Also the model is logged as well.<\/p>\n<p>If you&#8217;ve come this far, congrats!<\/p>\n<p>In the next part, we would then see how we can grab the text entered in the text field and use it to update our model.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will follow from the previous tutorial where actually rendered the model in the UI. Now we would be adding a TextBox and a &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-elm-programming-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Elm - Working With Button and TextBoxes - Elm Programming<\/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:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Elm - Working With Button and TextBoxes - Elm Programming\" \/>\n<meta property=\"og:description\" content=\"This tutorial will follow from the previous tutorial where actually rendered the model in the UI. Now we would be adding a TextBox and a &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/\" \/>\n<meta property=\"og:site_name\" content=\"Elm Programming\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-21T15:27:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-17T07:21:49+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\"},\"headline\":\"Elm &#8211; Working With Button and TextBoxes\",\"datePublished\":\"2021-12-21T15:27:49+00:00\",\"dateModified\":\"2022-01-17T07:21:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/\"},\"wordCount\":487,\"commentCount\":0,\"articleSection\":[\"Elm Programming Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/\",\"name\":\"Elm - Working With Button and TextBoxes - Elm Programming\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#website\"},\"datePublished\":\"2021-12-21T15:27:49+00:00\",\"dateModified\":\"2022-01-17T07:21:49+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-working-with-button-and-textboxes\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Elm &#8211; Working With Button and TextBoxes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/\",\"name\":\"Elm Programming\",\"description\":\"Elm Programming Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"sameAs\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\"],\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Elm - Working With Button and TextBoxes - Elm Programming","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:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/","og_locale":"en_US","og_type":"article","og_title":"Elm - Working With Button and TextBoxes - Elm Programming","og_description":"This tutorial will follow from the previous tutorial where actually rendered the model in the UI. Now we would be adding a TextBox and a &hellip;","og_url":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/","og_site_name":"Elm Programming","article_published_time":"2021-12-21T15:27:49+00:00","article_modified_time":"2022-01-17T07:21:49+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e"},"headline":"Elm &#8211; Working With Button and TextBoxes","datePublished":"2021-12-21T15:27:49+00:00","dateModified":"2022-01-17T07:21:49+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/"},"wordCount":487,"commentCount":0,"articleSection":["Elm Programming Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/","url":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/","name":"Elm - Working With Button and TextBoxes - Elm Programming","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/elm\/#website"},"datePublished":"2021-12-21T15:27:49+00:00","dateModified":"2022-01-17T07:21:49+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-working-with-button-and-textboxes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/elm\/"},{"@type":"ListItem","position":2,"name":"Elm &#8211; Working With Button and TextBoxes"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/elm\/#website","url":"https:\/\/kindsonthegenius.com\/elm\/","name":"Elm Programming","description":"Elm Programming Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/elm\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"sameAs":["https:\/\/kindsonthegenius.com\/elm"],"url":"https:\/\/kindsonthegenius.com\/elm\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":10,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":191,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/163\/revisions\/191"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}