<?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: Jesse</title>
    <description>The latest articles on DEV Community by Jesse (@jcooke).</description>
    <link>https://dev.to/jcooke</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%2F409802%2Fab8ee48c-26cb-4f74-b298-5273e36a8a7c.jpg</url>
      <title>DEV Community: Jesse</title>
      <link>https://dev.to/jcooke</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jcooke"/>
    <language>en</language>
    <item>
      <title>Use JavaScript to click a button for you</title>
      <dc:creator>Jesse</dc:creator>
      <pubDate>Tue, 16 Jun 2020 18:59:27 +0000</pubDate>
      <link>https://dev.to/jcooke/use-javascript-to-click-a-button-for-you-3mo1</link>
      <guid>https://dev.to/jcooke/use-javascript-to-click-a-button-for-you-3mo1</guid>
      <description>&lt;p&gt;So let's say you have a button on your website that you want to click a bunch of times for completely ethical purposes..&lt;/p&gt;

&lt;p&gt;I'm going to show you how to do just that with &lt;strong&gt;8 lines of code&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Let's get started
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Write a function
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;myFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Select the button and assign to variable
&lt;/h2&gt;

&lt;p&gt;There are going to be two ways to do this.&lt;/p&gt;
&lt;h4&gt;
  
  
  Select by ID
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;var button = document.getElementByID('mybutton');&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Select by Class
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;var button = document.getElementsByClassName('mybutton')[0];&lt;/code&gt;&lt;br&gt;
&lt;em&gt;In this example above, I select the first element using the class name specified using &lt;code&gt;[0]&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Checking in
&lt;/h2&gt;

&lt;p&gt;Your code should look like this now:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;myFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementByID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mybutton&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  For loop
&lt;/h2&gt;

&lt;p&gt;Next thing to do is setup a for loop. I could easily write a whole post on these but here's the syntax and a quick breakdown:&lt;br&gt;
&lt;code&gt;for (i = 0; i &amp;lt; 20; i++) {&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2Fgv8XQcv%2Fdevto-jesse-jsforloop.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2Fgv8XQcv%2Fdevto-jesse-jsforloop.jpg" alt="devto-jesse-jsforloop"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Click the button
&lt;/h2&gt;

&lt;p&gt;Finally, inside your for loop, you can tell your function to click the button using &lt;code&gt;.click()&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;button.click();&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  End result
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;myFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementByID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mybutton&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Run the function:&lt;/span&gt;
&lt;span class="nf"&gt;myFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>Control text excerpt length with JavaScript</title>
      <dc:creator>Jesse</dc:creator>
      <pubDate>Mon, 15 Jun 2020 22:10:12 +0000</pubDate>
      <link>https://dev.to/jcooke/control-text-excerpt-length-with-js-2m89</link>
      <guid>https://dev.to/jcooke/control-text-excerpt-length-with-js-2m89</guid>
      <description>&lt;h1&gt;
  
  
  Why not use CSS?
&lt;/h1&gt;

&lt;p&gt;The CSS version works just fine and there are actually a couple ways you can pull it off. But, it feels like a bit of a "hack" to me. Using JavaScript feels more purposeful and makes a little more sense in my opinion.&lt;/p&gt;

&lt;h1&gt;
  
  
  Let's get started
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Write a function
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;function myFunction() {&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The string
&lt;/h3&gt;

&lt;p&gt;You'll need to assign a variable for your string. For this example, I'll write the string in:&lt;br&gt;
&lt;code&gt;var str = "Killua Zoldyck is the best friend of Gon Freecss";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you're trying to pull all excerpts in your blog, you'll need to find the class name used and call that in:&lt;br&gt;
&lt;code&gt;var str = document.getElementByClassName("my_excerpt");&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The output
&lt;/h3&gt;

&lt;p&gt;Next, you'll need to define a space to insert your excerpt in the HTML:&lt;br&gt;
&lt;code&gt;var output = document.getElementById("excerpt_area");&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The if statement
&lt;/h3&gt;

&lt;p&gt;The first thing we want to do here is decide our character length. For this example, we'll set it to 10 characters.&lt;br&gt;
So, let's start by checking if our string is greater than 10 characters using &lt;code&gt;.length&lt;/code&gt;:&lt;br&gt;
&lt;code&gt;if (str.length &amp;gt; 10) {&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, let's tell our function what to do if that criteria is met. We'll need to only show part of the string using &lt;code&gt;.substring()&lt;/code&gt; which requires two arguments: a start and end point in the string. We'll tell it to start at character 0 and end at character 10:&lt;br&gt;
&lt;code&gt;str = str.substring(0,10);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Optional:&lt;br&gt;
I like to show the user that we're clipping the text by adding three periods. We can simply add on to our if statement:&lt;br&gt;
&lt;code&gt;str = str.substring(0,10) + "...";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lastly, we can close our if statement.&lt;/p&gt;

&lt;h3&gt;
  
  
  Showing the excerpt
&lt;/h3&gt;

&lt;p&gt;Using the output variable we defined, we can use &lt;code&gt;.innerHTML&lt;/code&gt; to print our excerpt:&lt;br&gt;
&lt;code&gt;output.innerHTML = str;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We place this outside the if statement so that any excerpts below 10 characters will still be shown in the HTML.&lt;/p&gt;

&lt;p&gt;The end result should show: "Killua Zol..."&lt;/p&gt;

&lt;h3&gt;
  
  
  Full code snippet:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Killua Zoldyck is the best friend of Gon Freecss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;excerpt_area&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>html</category>
    </item>
  </channel>
</rss>
