Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make this Javascript checkbox trigger action work like my CSS only example
    primarykey
    data
    text
    <p>I'm using the following code to test for "if" a checkbox is checked on page load. If it is, then a certain additional field will be shown (called myfield):</p> <pre><code>&lt;style&gt; #myfield { display: none; } &lt;/style&gt; &lt;input type="checkbox" id="mycheckbox" name="mycheckbox" /&gt; &lt;input type='text' id='myfield' name='myfield' /&gt; &lt;script&gt; if ($("#mycheckbox").is(":checked")) { document.getElementById("id").style.display="block"; } &lt;/script&gt; </code></pre> <p>However, this only works when the page loads and the checkbox is already checked. It doesn't work live when the box isn't checked on page load, and you go to click the box. I want the hidden field to show up right away when the box is "checked" without the page having to reload. I then want myfield to hide right away when the box is unchecked.</p> <p>Can any anyone point out the better/proper way to do this?</p> <p>Additionally:</p> <p>Of note: I do know how to do this in CSS using labels, but I need to use javascript other times.</p> <p>Here's what works fine in modern browsers using just CSS: <a href="http://jsfiddle.net/3KTC3/" rel="nofollow">http://jsfiddle.net/3KTC3/</a></p> <p>Here's that CSS only jsfiddle code:</p> <pre><code>&lt;style type="text/css"&gt; .label-for-check { display:none; } .check-with-label:checked + .label-for-check { display:block; } &lt;/style&gt; &lt;div&gt; &lt;input type="checkbox" id="check" class="check-with-label" /&gt; &lt;label for="check" class="label-for-check"&gt; &lt;br /&gt;&lt;br /&gt;MyField&lt;br /&gt; &lt;input type='text' id='myfield' name='myfield' size='10' /&gt; &lt;/label&gt; &lt;div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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