<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adnan Afzal</title>
    <description>The latest articles on DEV Community by Adnan Afzal (@adnanafzal565).</description>
    <link>https://dev.to/adnanafzal565</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F811064%2F4b0a2d13-795a-473b-8000-74991828cd92.jpg</url>
      <title>DEV Community: Adnan Afzal</title>
      <link>https://dev.to/adnanafzal565</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adnanafzal565"/>
    <language>en</language>
    <item>
      <title>2 methods to prevent form from submission</title>
      <dc:creator>Adnan Afzal</dc:creator>
      <pubDate>Fri, 11 Feb 2022 15:09:43 +0000</pubDate>
      <link>https://dev.to/adnanafzal565/2-methods-to-prevent-form-from-submission-2p43</link>
      <guid>https://dev.to/adnanafzal565/2-methods-to-prevent-form-from-submission-2p43</guid>
      <description>&lt;p&gt;Normally, when you submit the form, it redirects to the action attribute link. But you can stop that reload and call an AJAX or any other Javascript function.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. preventDefault
&lt;/h2&gt;

&lt;p&gt;You can call the &lt;strong&gt;preventDefault&lt;/strong&gt; function on the event target and it will stop the form from redirecting to the page specified in the action attribute of the &lt;strong&gt;&amp;lt;form&amp;gt;&lt;/strong&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form method="POST" action="your-page.php" onsubmit="onFormSubmit();"&amp;gt;
    &amp;lt;input type="submit" /&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can create the following Javascript function to prevent the page reload:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function onFormSubmit() {
    event.preventDefault();

    // your Javascript code here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this method, the form is first prevented from submission and then your Javascript code will be run.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. return false
&lt;/h2&gt;

&lt;p&gt;The second approach is to use the &lt;strong&gt;return false&lt;/strong&gt; statement in your Javascript code. For this, you also need to return the value from &lt;strong&gt;onsubmit&lt;/strong&gt; event function. Let's check this out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form method="POST" action="your-page.php" onsubmit="return onFormSubmit();"&amp;gt;
    &amp;lt;input type="submit" /&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then create your Javascript function as like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function onFormSubmit() {
    // your Javascript code here

    return false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this method, your Javascript code runs first before preventing the form submission.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recommendation
&lt;/h2&gt;

&lt;p&gt;We would recommend the 1st method i.e. &lt;strong&gt;preventDefault&lt;/strong&gt; because it is the very first line of the Javascript function. So if there is an error in your Javascript code, your browser will not get reloaded and thus you will be able to view the error in the browser console.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Display Messages in Browser Console using Figlet</title>
      <dc:creator>Adnan Afzal</dc:creator>
      <pubDate>Thu, 10 Feb 2022 18:24:34 +0000</pubDate>
      <link>https://dev.to/adnanafzal565/display-messages-in-browser-console-using-figlet-alo</link>
      <guid>https://dev.to/adnanafzal565/display-messages-in-browser-console-using-figlet-alo</guid>
      <description>&lt;p&gt;Here how you can display beautiful messages in a browser console using Javascript. You just need to download a library called &lt;a href="https://github.com/patorjk/figlet.js/"&gt;Figlet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After download, you need to extract the zip file and copy the file &lt;strong&gt;lib/figlet.js&lt;/strong&gt; in your project. You also need to copy the &lt;strong&gt;fonts&lt;/strong&gt; folder too.&lt;/p&gt;

&lt;p&gt;Then you need to include that library in your project using the script tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="figlet.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right after that, you can display the message you want in the browser console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
    // initialize the library

    const textDisplay = "adnan-tech.com";
    const font = "Speed";

    figlet(textDisplay, font, function (error, text) {
        // display the text in console
        console.log(text);
    });
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Create a Password Protected ZIP file in Mac OS X</title>
      <dc:creator>Adnan Afzal</dc:creator>
      <pubDate>Mon, 07 Feb 2022 19:21:12 +0000</pubDate>
      <link>https://dev.to/adnanafzal565/create-a-password-protected-zip-file-in-mac-os-x-3oh5</link>
      <guid>https://dev.to/adnanafzal565/create-a-password-protected-zip-file-in-mac-os-x-3oh5</guid>
      <description></description>
      <category>ios</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>zig</category>
    </item>
  </channel>
</rss>
