Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since this is a fairly basic question, I think it'll be instructional to walk you through the jQuery documentation while I answer your question. If you know truly nothing about jQuery, I recommend following this short tutorial first -- it will make things much, much easier for you in the future: <a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery" rel="nofollow noreferrer">jQuery Documentation - Getting Started With jQuery</a></p> <p>Your requirement is that something happens (in this case, another element is hidden/shown) when we click the radio buttons. There's two parts to this problem: first, we need to find the radio buttons, then we need to make something happen when we click it.</p> <p><strong>1. Finding the radio buttons</strong></p> <p>Take a look at the jQuery Selector documentation here: <a href="http://api.jquery.com/category/selectors/" rel="nofollow noreferrer">http://api.jquery.com/category/selectors/</a></p> <p>As you can see, there's a specific pseudo-selector for radio buttons, ":radio". We want to select everything inside of the element with ID "buttons", so this is how the selector will look in total:</p> <pre><code>$("#buttons input:radio"); </code></pre> <p>By the way, it's called a "pseudo-selector" because it filters items we've already selected (in this case, input tags inside of a div with id "button"). Pseudo-selectors always start with a ":".</p> <p><strong>2. Making something happen when we click them</strong></p> <p>Consult the jQuery Events reference here: <a href="http://api.jquery.com/category/events/" rel="nofollow noreferrer">http://api.jquery.com/category/events/</a></p> <p>We want the ".click()" event here, clearly. Once we've selected our elements, we can apply a click handler to them like this:</p> <pre><code>$("#buttons input:radio").click(function() { // make something happen here alert("input button clicked: " + $(this).index()); }); </code></pre> <p>Note that this will apply the same click handler to all three of the input buttons, but you can access the specific element that was clicked via the "this" keyword. Wrapping $() around it makes it into a jQuery selection rather than just a Javascript object and allows us to call jQuery functions on it.</p> <p><strong>3. Hiding and showing the list element conditionally</strong></p> <p>Let's extend the code above to actually hide and show that other div, depending on which item was clicked. We're going to refer to the jQuery Effects documentation so that we can make hiding and showing it exciting: <a href="http://api.jquery.com/category/effects/" rel="nofollow noreferrer">http://api.jquery.com/category/effects/</a></p> <p>The functions we'll be using are ".slideUp()", which hides an element, and ".slideDown()", which shows it. We'll also be using the ".index()" function I used in the previous example to figure out which button was clicked, although I recommend giving the button a unique ID in the future so that your code isn't dependent on the order of the buttons. Here's the final code:</p> <pre><code>$("#buttons input:radio").click(function() { // if it was the third button (0-indexed, so the 3rd one is index 2)... if ($(this).index() == 2) { // display the element with ID "list" $("#list").slideDown(); } else { // hide the element with ID "list" $("#list").slideUp(); } }); </code></pre> <p>Sorry for the length of this answer, but hopefully it was more conducive to your understanding of jQuery than "copy and paste this 3-line super-compact solution".</p>
    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.
    1. VO
      singulars
      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