title: How to condense similar JavaScript functions into one?
body: <p>I have about 30 scripts that are nearly identical except for small differences. I'm wondering if they can all be combined into one. </p>
  
  <p>For instance, here is the HTML which calls on the functions "Writedata" 1 through 3. The remaining 27 are not shown:</p>
  
  <pre><code>&lt;div class="trashitem" onclick="Writedata1()"&gt;
    &lt;div class="photobox"&gt;
      &lt;img src="http://www.fakeimage.jpg"&gt;
      &lt;input type="hidden" name="write" value="4562711" id="mls_id1"&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="trashitem" onclick="Writedata2()"&gt;
    &lt;div class="photobox"&gt;
      &lt;img src="http://www.fakeimage.jpg"&gt;
      &lt;input type="hidden" name="write" value="7436743" id="mls_id3"&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="trashitem" onclick="Writedata3()"&gt;
    &lt;div class="photobox"&gt;
      &lt;img src="http://www.fakeimage.jpg"&gt;
      &lt;input type="hidden" name="write" value="114992" id="mls_id3"&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>They each have a unique id with a unique value, which is why I had separate functions being called. The respective scripts are:</p>
  
  <pre><code>&lt;script language="javascript"&gt;
    function Writedata1()
    {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var write_id;
      write_id = document.getElementById('write_id').value ;
      var s = fso.CreateTextFile(write_id, true);
      s.WriteLine("RestoreClicked");
      s.WriteLine(document.getElementById('mls_id1').value);
      s.Close();
    }
  &lt;/script&gt;
  
  &lt;script language="javascript"&gt;
    function Writedata2()
    {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var write_id;
      write_id = document.getElementById('write_id').value ;
      var s = fso.CreateTextFile(write_id, true);
      s.WriteLine("RestoreClicked");
      s.WriteLine(document.getElementById('mls_id2').value);
      s.Close();
    }
  &lt;/script&gt;
  
  &lt;script language="javascript"&gt;
    function Writedata3()
    {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var write_id;
      write_id = document.getElementById('write_id').value ;
      var s = fso.CreateTextFile(write_id, true);
      s.WriteLine("RestoreClicked");
      s.WriteLine(document.getElementById('mls_id3').value);
      s.Close();
    }
  &lt;/script&gt;
  </code></pre>
  
  <p>Is there a script I can use instead which will apply to all functions named in the format Writedata#()  where <code>#</code> is any number. I can't have the script being limited to 30 only, it should be dynamic. Pure JavaScript only please.</p>
  
  <hr>
  
  <p><strong>UPDATED QUESTION</strong></p>
  
  <hr>
  
  <p>Here is the full code, even when you click a trashitem div, it doesn't write the text file "keepornew.txt" as it should. It worked before including the script Shomz and Nina provided. Are you able to spot the problem? Thank you</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="X-UA-Compatible" content="IE=Edge" /&gt;
  &lt;title&gt;Trash&lt;/title&gt;
  
  &lt;SCRIPT LANGUAGE="VBScript"&gt;
  
      Sub Window_onLoad
          window.resizeTo 175,1031
         window.moveto 0,0
      End Sub 
  
  &lt;/SCRIPT&gt;
  
  &lt;HTA:APPLICATION icon="C:\Users\ME\Desktop\Abacus\menuIcon.ico"
       APPLICATIONNAME="WELCOME"
       SCROLL="yes
       SINGLEINSTANCE="no"
       WINDOWSTATE="normal" 
       ShowInTaskBar="no"
       MinimizeButton="yes"
      MaximizeButton="no"
      RESIZE="no"
      CAPTION="no"
  &gt; 
  
  &lt;style&gt;
  body{background-color:#ffffff;}
  body{background-color:#ffffff;}
  
  .bordered{
    border: 3px solid #D64631;
  }
  
  .trashitemborder{
   border: 1px solid black;
    width:100%;
    height: 140px;
    margin-top:10px;
  
  }
  .trashitem {
      width:100%;
      height:110px;
    margin-left:0px;
    overflow:hidden;
  }
  
  .photobox{100%;
    text-align:center;
  }
  .photobox img{width:100%;
  }
  
  .pricebox{
    font-family:arial;
    font-size:15px;
    font-weight:bold;
  margin-left:25px;
  }
  
  .trashtitle{font-family:arial;
  font-size:20px;
  color:#D64631;
  text-align:center;
  margin-top:-25px;
  }
  .trashcaption{font-family:arial;
  font-size:12px;
  color:#D64631;
  margin-left:-2px;
  margin-bottom:9px;
  text-align:center;
  }
  
  &lt;/style&gt;
  &lt;script language="javascript"&gt;
  function Writedata(id)
  {
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var write_id;
    write_id = document.getElementById('write_id').value ;
    var s = fso.CreateTextFile(write_id, true);
    s.WriteLine("RestoreClicked");
    s.WriteLine(document.getElementById('mls_id' + id).value);
    s.WriteLine(document.getElementById('marker_id' + id).value);
    s.Close();
  }
  &lt;/script&gt;
  
  &lt;script language="javascript"&gt;
  window.onload = function (event) {
  
      var myitem = document.querySelectorAll(".trashitem");
      for(h=0; h&lt;myitem.length; h++){
          myitem[h].onclick = function(e){
              var myitem = document.querySelectorAll(".trashitem");
  
              for(h=0; h&lt;myitem.length; h++){
                  myitem[h].classList.remove("bordered");
              }
              this.classList.add("bordered");
  
          };
      }
  };
  &lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;input type="hidden" name="write" value="C:\Users\ME\Documents\Temp\keepornew.txt" id="write_id"&gt;&lt;br/&gt;
  
  &lt;div class="trashtitle"&gt;TRASH&lt;/div&gt;
  &lt;div class="trashcaption"&gt;Click comp to restore&lt;/div&gt;
  
  &lt;input type="hidden" name="filler" value="filler" id="filler_id"&gt;
  &lt;input type="hidden" name="MarkerA4" value="MarkerA4"&gt;
  &lt;div class="trashitemborder"&gt;
  &lt;div class="trashitem" onclick="Writedata(4)"&gt;
      &lt;div class="photobox"&gt;
       &lt;img src="http://www.fakeimagecom/image1.JPG"&gt;
      &lt;/div&gt;
   &lt;input type="hidden" name="write" value="5189975" id="mls_id4"&gt;
  &lt;input type="hidden" name="write" value="4" id="marker_id4"&gt;
       &lt;div class="pricebox"&gt;$325,000&lt;/div&gt;  
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;input type="hidden" name="MarkerB4" value="MarkerB4"&gt;
  &lt;input type="hidden" name="MarkerA3" value="MarkerA3"&gt;
  &lt;div class="trashitemborder"&gt;
  &lt;div class="trashitem" onclick="Writedata(3)"&gt;
      &lt;div class="photobox"&gt;
         &lt;img src="http://www.fakeimagecom/image1.JPG"&gt;
      &lt;/div&gt;
   &lt;input type="hidden" name="write" value="5158764" id="mls_id3"&gt;
  &lt;input type="hidden" name="write" value="3" id="marker_id3"&gt;
       &lt;div class="pricebox"&gt;$320,000&lt;/div&gt;  
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;input type="hidden" name="MarkerB3" value="MarkerB3"&gt;
  &lt;input type="hidden" name="MarkerA2" value="MarkerA2"&gt;
  &lt;div class="trashitemborder"&gt;
  &lt;div class="trashitem" onclick="Writedata(2)"&gt;
      &lt;div class="photobox"&gt;
        &lt;img src="http://www.fakeimagecom/image1.JPG"&gt;
      &lt;/div&gt;
   &lt;input type="hidden" name="write" value="201500274" id="mls_id2"&gt;
  &lt;input type="hidden" name="write" value="2" id="marker_id2"&gt;
       &lt;div class="pricebox"&gt;$302,500&lt;/div&gt;  
  &lt;/div&gt;
  
  &lt;input type="hidden" name="MarkerB2" value="MarkerB2"&gt;
  &lt;input type="hidden" name="MarkerA1" value="MarkerA1"&gt;
  &lt;div class="trashitem" onclick="Writedata(1)"&gt;
      &lt;div class="photobox"&gt;
          &lt;img src="http://www.fakeimagecom/image1.JPG"&gt;
      &lt;/div&gt;
   &lt;input type="hidden" name="write" value="5076992" id="mls_id1"&gt;
  &lt;input type="hidden" name="write" value="1" id="marker_id1"&gt;
       &lt;div class="pricebox"&gt;$375,000&lt;/div&gt;  
  &lt;/div&gt;
  &lt;input type="hidden" name="MarkerB1" value="MarkerB1"&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>