Note that there are some explanatory texts on larger screens.

plurals
  1. POAltering a page from a Greasemonkey script whenever a specific <option> is selected
    primarykey
    data
    text
    <p>While writing a GreaseMonkey script using jQuery and "tamtam", a question arose in my mind.</p> <p>There is a page (which I can't edit) having a <code>&lt;select&gt;</code> element with four options. Option 2 is preselected.</p> <pre><code>&lt;select id="idname" name="namename"&gt; &lt;option&gt;Option1&lt;/option&gt; &lt;option selected="selected"&gt;Option2&lt;/option&gt; &lt;option&gt;Option3&lt;/option&gt; &lt;option&gt;Option4&lt;/option&gt; &lt;/select&gt; </code></pre> <p>Now I'd like to call the script when Option1 is selected. Option1 leads to another site I want to insert my script.</p> <p>How would I write something like:</p> <pre><code>if (Option1 is selected) { perform the script } </code></pre> <p>Would something like the following work?</p> <pre><code>if(document.getElementById("idname").getElementsByTagName("option")[0].onselect == true){ perform the script } </code></pre> <p>If not, can you post a reference helping me?</p> <p><strong>Edit</strong></p> <p>I was able to create a function and an event handler.</p> <pre><code>function myCompute(Event) { with (this) { var myTest = value; if (value == "option1") { $("#tabelle").show(); } else { $("#tabelle").hide(); } } } </code></pre> <p>Event handlers:</p> <pre><code>$("#option-type").change (myCompute); $("#tabelle").hide(); </code></pre> <p>It works as follows: By choosing option 2,3 or 4 the table is hidden. By choosing option 1 the table is shown. By visiting the site option 2 is selected most of the time and nothing is shown. Now I got the case that option 1 is selected by visiting the site and no table appears, too. My idea was that the table should be shown when option1 is preselected.. I think that an EventHandler is missing. Like you, Brock Adams, said.</p> <pre><code>$("#option-type option:first").select (myCompute); $("#option-type").change (myCompute); $("#tabelle").hide(); </code></pre> <p>If I bind the function with <code>$("#tabelle").hide();</code>, the table is hidden from the very beginning. By changing the options to option1 the table is shown. How can I show the table when option 1 is selected and how can I hide the table when option 2,3,4 are selected?</p> <p>Trying <code>option:first</code> results in an "unknown pseudo-element" error.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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