943,696 Members | Top Members by Rank

Ad:
Jul 30th, 2008
0

How to determine which button was pressed?

Expand Post »
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);
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n00b0101 is offline Offline
1 posts
since Jul 2008
Aug 1st, 2008
0

Re: How to determine which button was pressed?

you can add onclick="your show form function" to the inputs
Reputation Points: 12
Solved Threads: 3
Newbie Poster
jel3 is offline Offline
18 posts
since May 2008
Sep 1st, 2008
0

Re: How to determine which button was pressed?

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...
html Syntax (Toggle Plain Text)
  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>


javascript Syntax (Toggle Plain Text)
  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>
html Syntax (Toggle Plain Text)
  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>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Validating email with javascrip problem
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Tooltips for feed description





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC