How to determine which Submit button was clicked?

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
thepip3r
Posts: 5
Joined: Tue Aug 24, 2010 7:22 am

How to determine which Submit button was clicked?

Post by thepip3r » Tue Aug 24, 2010 7:22 am

...on dynamically generated submit buttons? Basically, when I dynamically create the submit buttons, the "onclick" parameter won't allow me to pass a function with a parameter so all of my created submit buttons all have the same function. So my question is, within that function, how do I determine which of my 15 submit buttons were clicked so I can take the appropriate action on the one being run? Thanks in advance. Here is the javascript portion of what I've got so far:

Code: Select all

<script type="text/javascript"> 
   
  var mySites = new Array(); 
  mySites[0] = ['site1', '\uncpath']; 
  mySites[1] = ['site1', '\uncpath']; 
  mySites[2] = ['site1', '\uncpath']; 
  mySites[3] = ['site1', '\uncpath']; 
  mySites[4] = ['site1', '\uncpath']; 
  mySites[5] = ['site1', '\uncpath']; 
  mySites[6] = ['site1', '\uncpath']; 
  mySites[7] = ['site1', '\uncpath']; 
  mySites[8] = ['site1', '\uncpath']; 
  mySites[9] = ['site1', '\uncpath']; 
  mySites[10] = ['site1', '\uncpath']; 
  mySites[11] = ['site1', '\uncpath']; 
  mySites[12] = ['site1', '\uncpath']; 
  mySites[13] = ['site1', '\uncpath']; 
  mySites[14] = ['site1', '\uncpath']; 
  mySites[15] = [site1', '\uncpath']; 
   
  function loadButtons() { 
   var myDiv = document.getElementById("divMain"); 
   var myTable = document.createElement("table"); 
   myTable.setAttribute('id', "sites"); 
   var myTbody = document.createElement("tbody"); 
     
   for (var i=0; i < mySites.length; i++) { 
    var myTr = document.createElement("tr"); 
    var myTd1 = document.createElement("td"); 
    var myTd2 = document.createElement("td"); 
    var myTd3 = document.createElement("td"); 
     
    myTd1.appendChild(document.createTextNode(mySites<EM>[0])); 
    myTr.appendChild(myTd1); 
     
    myTd2.appendChild(document.createTextNode(mySites<EM>[1])); 
    myTr.appendChild(myTd2); 
     
    var myButton = document.createElement('input'); 
    myButton.type = 'button'; 
    myButton.name = mySites<EM>[1]; 
    myButton.value = "Update"; 
    myButton.onclick = updateDefFiles;  <------------ 
    myTd3.appendChild(myButton); 
    myTr.appendChild(myTd3); 
     
    myTbody.appendChild(myTr); 
    myTable.appendChild(myTbody); 
    myDiv.appendChild(myTable); 
   } 
  } 
   
  function updateDefFiles() { 
   alert("hiyo"); 
  } 
   
 </script>

If I could somehow, I'd love to change this: myButton.onclick = updateDefFiles; to this: myButton.onclick = updateDefFiles(mySites[0]);

But I don't believe this can be done (at least from what I've read on my Google searches). So I think I'm stuck with just using my updateDefFiles() generic function. But once that function is called, how can I determine which of my submit buttons was clicked? TIA!

thepip3r2010-08-24 14:26:30

User avatar
jvierra
Posts: 13281
Joined: Tue May 22, 2007 9:57 am
Contact:

How to determine which Submit button was clicked?

Post by jvierra » Tue Aug 24, 2010 7:45 am

One other thing. YU can create a cunction pointer with getCall(func,arg,arg,arg,,,)

assign this to teh onclick event.


User avatar
thepip3r
Posts: 5
Joined: Tue Aug 24, 2010 7:22 am

How to determine which Submit button was clicked?

Post by thepip3r » Tue Aug 24, 2010 8:05 am

So based off of what I could discern from your post, I tried:

Code: Select all

    
    var myButton = document.createElement('input');
    myButton.type = 'button';
    myButton.name = mySites[1];
    myButton.value = "Update";
    myButton.onclick = "return updateDefFiles(" + mySites[0] + ");"
    myTd3.appendChild(myButton);
    myTr.appendChild(myTd3);
    
    myTbody.appendChild(myTr);
    myTable.appendChild(myTbody);
    myDiv.appendChild(myTable);
   }
  }
  
  function updateDefFiles(myVal) {
   alert(myVal);
  }

And the array value is not showing up in a popup when I click one of my buttons. I also tried removing the concatenation operators (+) and just use single quotes instead but that didn't work either. Any suggestions?

User avatar
jvierra
Posts: 13281
Joined: Tue May 22, 2007 9:57 am
Contact:

How to determine which Submit button was clicked?

Post by jvierra » Tue Aug 24, 2010 9:10 am

"this" is an object so you need to specifiy a property.

Try alert(myvar.tagname);

User avatar
jvierra
Posts: 13281
Joined: Tue May 22, 2007 9:57 am
Contact:

How to determine which Submit button was clicked?

Post by jvierra » Tue Aug 24, 2010 10:06 am

You need to post you complete script. I have no way of kowing what you are trying to do without it.

Is this an HTA?

Why are you generating this in script? Why done' you just code it in HTML.

User avatar
thepip3r
Posts: 5
Joined: Tue Aug 24, 2010 7:22 am

How to determine which Submit button was clicked?

Post by thepip3r » Tue Aug 24, 2010 10:27 am

right and i totally understand how simple it is when you manually set the functions inline to the HTML but that's the point. I need it to be dynamically generated so that anyone else who needs to add or remove a site will simply have to update the array and then everything else will fall in place... here is my script as it currently stands:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "<a href="http://www.w3.org/TR/html4/strict.dtd" target="_blank">http://www.w3.org/TR/html4/strict.dtd</A>"> 
<html>
<head>
 <title>AV Virus Definition File Updater</title>
 <HTA:APPLICATION 
  ID="oVirUpdater" 
  APPLICATIONNAME="oVirUpdater"
  SCROLL="yes"
  SINGLEINSTANCE="no"
  WINDOWSTATE="normal"
 >
 
 <style type="text/css">
  body, html {
   margin:0;
   padding:10px 10px 10px 10px;
  }
  
  input {
   border: 1px solid #000;
   cursor:hand;
  }
   
  #sites {
   border-collapse:collapse;
   font: 12px "Trebuchet MS", Verdana, Arial, Helvetica,sans-serif;
  }
  
  td {
   border: 1px solid #aaa;
   padding: 3px 30px 3px 30px;
   letter-spacing:1px;
  }
  
  textarea {
   border:1px solid #aaa;
   background:#eee;
  }
 
 </style>
 
  <script type="text/javascript">
  
  var mySites = new Array();
  mySites[0] = ['site1', '\uncpath'];
  mySites[1] = ['site1', '\uncpath'];
  mySites[2] = ['site1', '\uncpath'];
  mySites[3] = ['site1', '\uncpath'];
  mySites[4] = ['site1', '\uncpath'];
  mySites[5] = ['site1', '\uncpath'];
  mySites[6] = ['site1', '\uncpath'];
  mySites[7] = ['site1', '\uncpath'];
  mySites[8] = ['site1', '\uncpath'];
  mySites[9] = ['site1', '\uncpath'];
  mySites[10] = ['site1', '\uncpath'];
  mySites[11] = ['site1', '\uncpath'];
  mySites[12] = ['site1', '\uncpath'];
  mySites[13] = ['site1', '\uncpath'];
  mySites[14] = ['site1', '\uncpath'];
  mySites[15] = ['site1', '\uncpath'];
  
  function loadButtons() {
   var myDiv = document.getElementById("divMain");
   var myTable = document.createElement("table");
   myTable.setAttribute('id', "sites");
   var myTbody = document.createElement("tbody");
    
   for (var i=0; i < mySites.length; i++) {
    var myTr = document.createElement("tr");
    var myTd1 = document.createElement("td");
    var myTd2 = document.createElement("td");
    var myTd3 = document.createElement("td");
    
 myTd1.appendChild(document.createTextNode(mySites<em>[0]));
 myTr.appendChild(myTd1);</DIV>
       <DIV> myTd2.appendChild(document.createTextNode(mySites<em>[1]));
 myTr.appendChild(myTd2);
    
    var myButton = document.createElement('input');
    myButton.type = 'button';
    myButton.name = mySites[1];
    myButton.value = "Update";
    myButton.onclick = updateDefFiles;
    myTd3.appendChild(myButton);
    myTr.appendChild(myTd3);
    
    myTbody.appendChild(myTr);
    myTable.appendChild(myTbody);
    myDiv.appendChild(myTable);
   }
  }
  
  function updateDefFiles() {
   alert("hiyo");
  }
  
 </script>
 
 <script type="text/vbscript">
  Sub window_onload
   loadButtons
  End Sub
 </script>
 
</head>
<body>
 <div id="divMain">
 </div>
 
 <br />
 <input type="button" name="all" value="Update All">
 <br /><br />
 
 Log output:
 <br />
 <textarea id="log" rows="15" cols="75" readonly></textarea>
</body>
</html>

now in my normal script the "sites" and "UNC paths" are all unique.


EDIT: code updated to actually work.thepip3r2010-08-24 17:37:25

User avatar
jvierra
Posts: 13281
Joined: Tue May 22, 2007 9:57 am
Contact:

How to determine which Submit button was clicked?

Post by jvierra » Tue Aug 24, 2010 1:56 pm

Her eis acode sample of table generation.

The URL can point to a web source or a local file.

Code: Select all

<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> 
<html> 
<head> 
     <title>repeated table binding with the tdc</title> 
</head>
<body> 
<div class="body"> 
  
<object id="elements" classid="clsid:333c7bc4-460f-11d0-bc04-0080c7055a83"> 
 <param name="dataurl" value="/workshop/samples/author/databind/minielts.txt"> 
 <param name="useheader" value="true"> 
</object> 
  
<table datasrc="#elements"> 
     <thead> 
          <tr style="font-weight:bold"> 
               <td>element</td> 
               <td>symbol</td> 
          </tr> 
     </thead> 
     <tbody> 
          <tr> 
               <td><div datafld="element"></div></td> 
               <td><div datafld="symbol"></div></td> 
          </tr> 
     </tbody> 
</table></strong></DIV>
       <DIV><strong></div> 
</body> 
</html>

This is a two column table. Just add an onclick="doclick(me)" and it will call teh sub with the row object.

The value of the row element is:
object.firstChild.firstChild.innerText

This will display the name of the element in the first column of the row.

This is very easy to build and to repeat. It also allows for much easier editing and helps to externalize the data,

If you MUST gave the data in teh same file then we would use the DSO to store the data. THis is less fragile and more flexible than using an array and it canbe automatically loaded into a table in exactly the same way.
jvierra2010-08-24 21:10:20

User avatar
jvierra
Posts: 13281
Joined: Tue May 22, 2007 9:57 am
Contact:

How to determine which Submit button was clicked?

Post by jvierra » Wed Aug 25, 2010 6:29 am

Here is an example of a simple file load using the FIleSystemObject. This is very easy and quick for simple lists. It does not reauire any data enabling.

uploads/2491/table_addrows.hta.txt

This file will npot run by clicking the link. It must be run locally as an HTA. Save teh file and rename to HTA.

It is preset to look for test.txt but you can type in any text file name path that you have available and it will load it into a table. The table will be fully evented.


User avatar
thepip3r
Posts: 5
Joined: Tue Aug 24, 2010 7:22 am

How to determine which Submit button was clicked?

Post by thepip3r » Wed Aug 25, 2010 8:11 am

Well - I was close. It's been awhile since I buond a function to a javascript event.

Here is a snippet with changes highlighted.

Code: Select all

	
    myButton.onclick =  function (){updateDefFiles(this)}; // this declares a function pointer with argumnents.  It's actually easier in jscript.
    myTd3.appendChild(myButton);
    myTr.appendChild(myTd3);
    
    myTbody.appendChild(myTr);
    myTable.appendChild(myTbody);
    myDiv.appendChild(myTable);
   }
  }
  
  function updateDefFiles(el) {
   alert(el.tagName);
  }
  


@jvierra -- thank you so much. i had to modify it just slightly because .tagname was coming up with "undefined" on button click so I changed it to alert(myVal.name) and it shows exactly what I was looking for.

Thank you kindly for your massive continued effort. I apologize if my explanations were sub par. Thank you again for your assistance with this.

User avatar
thepip3r
Posts: 5
Joined: Tue Aug 24, 2010 7:22 am

How to determine which Submit button was clicked?

Post by thepip3r » Wed Aug 25, 2010 9:20 am

I use powershell as well but the unfortunate thing is, I'm not as familiar with it as I am vbscript/html/javascript so building a front end to a script is something I haven't tackled yet in PS. Plus, mshta is on all windows systems for some time, you have to rely on an installed version of PS for that to work as well. The idea for this project is to "stupid-proof" it as much as possible. Again, thank you for your assistance with this jvierra. I greatly appreciate it.

Locked