javascript - Hide an input until a checkbox has been checked -


in following form how can hide <input> tag after checkbox , show when checkbox checked?

<ul style="list-style-type:none">     <li>         <input type="text" name="accountname" style="width:95%" class="field-style field-full align-none" placeholder="company name" />     </li>     <li>         <label>please fill in contact information below</label>         <input type="text" name="firstname" class="field-style field-split align-left" placeholder="first name" />         <input type="text" name="lastname" class="field-style field-split align-right" placeholder="last name" />     </li>     <li>         <input type="text" name="address" class="field-style field-split align-left" placeholder="street # , name" />         <select placeholder="street type" style="height:33" class="field-style field-split align-center">             <option value="" disabled selected>street type</option>             <option value="st.">street</option>             <option value="rd.">road</option>             <option value="way">way</option>         </select>         <select placeholder="direction" style="height:33" class="field-style field-split align-right">             <option value="" disabled selected>direction (if applicable)</option>             <option value="n">north</option>         </select>     </li>     <li>         <select placeholder="state" style="height:33" class="field-style field-split align-left">             <option value="" disabled selected>state</option>             <option value="al">alabama</option>             <option value="ak">alaska</option>         </select>         <input type="text" name="street name" placeholder="zip code" class="field-style field-split align-right" />     </li>     <li>         <label>telephone number</label>         <input type="text" class="tel-number-field" name="tel_no_1" value="" maxlength="4" />-         <input type="text" class="tel-number-field" name="tel_no_2" value="" maxlength="4" />-         <input type="text" class="tel-number-field" name="tel_no_3" value="" maxlength="10" />     </li>     <li>         <input type="email" name="email" placeholder="e-mail address" />     </li>     <!-- hide/show input element below checkbox -->     <input type="checkbox" />     </br>     </br>     <li>         <button type="submit" style="width:95%">submit account</button>     </li> </ul> 

hiding , showing sibling elements using css

(see code corrections section below more information on corrections not specified in section)

if want able show , hide <input> tag after checkbox, there css solution.

simply append input tag <li> tag, use css ~ general sibling combinator in following code hide , show <input> tag according :checked state of checkbox

input[type="checkbox"] ~ input {      display: none;  }  input[type="checkbox"]:checked ~ input {      display: inline-block;  }
<ul style="list-style:none">       <li>          <input type="checkbox" />           <br /><br />          <input type="text">      </li>  </ul>


code corrections

the direct descendant elements allowed within <ul> tag <li> tags.

there no such thing </br> tag, there <br> tag optional (in html4 , html5) self-ending flag <br />.

this means following code should change

<ul>     ...     <input type="checkbox" />     </br></br>     ... </ul> 

to following code

<ul>     ...     <li>         <input type="checkbox" />         <br /><br />     </li>     .... </ul> 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -