How to determine which button was pressed?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2008
Posts: 1
Reputation: n00b0101 is an unknown quantity at this point 
Solved Threads: 0
n00b0101 n00b0101 is offline Offline
Newbie Poster

How to determine which button was pressed?

 
0
  #1
Jul 30th, 2008
I'm trying to determine which button was pressed when I'm unsure of how many buttons there actually are...

The smarty template that I'm using creates the input buttons based on a query that happens in the associated php script. I don't know how many input fields will actually be generated, otherwise, I'd just add an event listener for all of them. But, I can't seem to get the yui code to "figure out" which button was pressed. When the user presses one of the buttons, then a form should display.

This is the smarty code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. {foreach from=$value.sample_query key=key item=value2 name="samp"}
  2. {foreach from=$value2 item=value3}
  3. {$value3} <input type="button" id="sample{$key}" name="sample[{$key}]" value="" />
  4. {/foreach}
  5. {/foreach}

So, what results is something that looks like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="button" id="sample0" name="sample[0]" value="" />
  2. <input type="button" id="sample1" name="sample[1]" value="" />
  3. <input type="button" id="sample2" name="sample[2]" value="" />
  4. <input type="button" id="sample3" name="sample[3]" value="" />

The YUI code is below, which will be used to popup a form depending upon which button was pressed. Wherever there's a reference to "sample0" in the code below, *that's* what needs to change to the id of the button that was actually pressed.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. YAHOO.namespace("forms.container");
  2.  
  3. function init() {
  4.  
  5. // Define various event handlers for Dialog
  6. var handleSubmit = function() {
  7. this.submit();
  8. };
  9. var handleCancel = function() {
  10. this.cancel();
  11. };
  12. var handleSuccess = function(o) {
  13. var response = o.responseText;
  14. response = response.split("<!")[0];
  15. document.getElementById("resp").innerHTML = response;
  16. };
  17. var handleFailure = function(o) {
  18. alert("Submission failed: " + o.status);
  19. };
  20.  
  21. // Instantiate the Dialog
  22. YAHOO.forms.container.sample0 = new YAHOO.widget.Dialog("sample0",
  23. { width : "330px",
  24. fixedcenter : true,
  25. zindex: 100,
  26. visible : false,
  27. constraintoviewport : false,
  28. buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
  29. { text:"Cancel", handler:handleCancel } ]
  30. });
  31.  
  32. // Wire up the success and failure handlers
  33. YAHOO.forms.container.sample0.callback = { success: handleSuccess,
  34. failure: handleFailure };
  35.  
  36.  
  37. // Render the Dialog
  38. YAHOO.forms.container.sample0.render();
  39.  
  40. YAHOO.util.Event.addListener("showsample0", "click", YAHOO.forms.container.sample0.show, YAHOO.forms.container.sample0, true);
  41. YAHOO.util.Event.addListener("hidesample0", "click", YAHOO.forms.container.sample0.hide, YAHOO.forms.container.sample0, true);
  42.  
  43. }
  44.  
  45. YAHOO.util.Event.onDOMReady(init);
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 18
Reputation: jel3 is an unknown quantity at this point 
Solved Threads: 3
jel3's Avatar
jel3 jel3 is offline Offline
Newbie Poster

Re: How to determine which button was pressed?

 
0
  #2
Aug 1st, 2008
you can add onclick="your show form function" to the inputs
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: How to determine which button was pressed?

 
0
  #3
Sep 1st, 2008
Please feel free to modify this code to match your needs. If you have any question regarding this code you can document.write('Me on my inbox'). lol! Have a good day...
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Some title</titles>


  1. <script type="text/javascript">
  2. <!-- BEGIN HIDING
  3. /* All codes are tested before release, so you are well assured that all of my codes are hustle free...
  4. This example will determine which button is clicked.
  5. code is as follows: */
  6.  
  7. document.onclick = function( whichOne )
  8. { whichOne = whichOne ? whichOne : window.event;
  9. thisButton = whichOne.target ? whichOne.target : whichOne.srcElement;
  10.  
  11. if (( thisButton.name ) && ( thisButton.name == 'button1' )) { alert( '\n' + thisButton.title + ' was clicked!' ); }
  12. }
  13. // DONE HIDING -->
  14. </script>
  1. </head>
  2. <body>
  3. <p>
  4. <form>
  5. <input type="button" name="button1" title="Button One" value="Button 1" />
  6. <input type="button" name="button1" title="Button Two" value="Button 2" />
  7. <input type="button" name="button1" title="Button Three" value="Button 3" />
  8. <input type="button" name="button1" title="Button Four" value="Button 4" />
  9. </form>
  10. </p>
  11. </body>
  12. </html>
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC