Note that there are some explanatory texts on larger screens.

plurals
  1. POKnowing what button is pressed in Javascript
    text
    copied!<p>I was wondering if there was a way I could ask if a certain button was pushed so that it would do a certain thing in the function. I know I could split this up into two separate functions, but I want to know how to do this this way. </p> <p>I'll put my code below. </p> <pre><code>function displaySort(form) { var list = form.values.value.replace(/\s+/g, '') list = list.split(/,/) if (document.getElementById("fSort").form.input.id) { list.sort() var listAsString = list.join(", ") form.sortedDisplay.value = listAsString } else if (document.getElementById("rSort").form.input.id) { list.sort(reverseCompare) var listAsString = list.join(", ") form.reverseSortedDisplay.value = listAsString } window.alert("Messed Up"); } function reverseCompare(s1, s2) { if (s1 &lt; s2) { return +1 } else if ( s1 == s2 ){ return 0 } else { return -1 } } </code></pre> <p>the form:</p> <pre><code>&lt;form&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="text" name="values" value=" Bob, Ted, Carol, Bette, Alice, Peter " onclick="if (this.value == this.defaultValue) this.value=''" size="80" /&gt; &lt;input type="button" onclick="displaySort(this.form)" value="Sort" id="fSort" /&gt; &lt;input type="button" onclick="displaySort(this.form)" value="Reverse Sort" id="rSort" /&gt; &lt;input type="reset" value="Reset Form" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="text" name="sortedDisplay" value="Sorted list will show here" size="35" readonly style="font-size:24pt;font-weight:normal;" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="text" name="reverseSortedDisplay" value="Reverse sorted list will show here" size="35" readonly style="font-size:24pt;font-weight:normal;" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload