SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Trying to loop through radio buttons in jQuery...

    The Issue:

    I have the following form:
    Code:
    <form id="form_name" ...>
       <input type="radio" name="type[value]" value="GroupA" />
       <input type="radio" name="type[value]" value="GroupB"  checked="checked" />
       <input type="radio" name="type[value]" value="GroupC" />
       <input type="radio" name="type[value]" value="GroupD" />
       <input type="radio" name="type[value]" value="GroupE" />
    </form>
    I'm trying to loop over each radio to determine if one of the first two inputs (GroupA or GroupB) has the checked attribute with "checked" as its values, regardless of whether it's before or after page-load. So, I need this logic to occur during page load as well as after the page has loaded to check if someone changes the value. Pretty straightforward (I hope).

    Here's what I have so far:
    Code:
        $("form#form_name input:radio").each(function(){
            $(":radio[value=GroupA]").click(function(){
                $("fieldset.group-manual-entry.collapsible").css('display','block');
                alert('Group A');
                return;
            });
    
            $(":radio[value=GroupB]").click(function(){
                $("fieldset.group-manual-entry.collapsible").css('display','block');
                alert('Group B');
                return;
            });
            return false;
        });
    I've been told that it's bad practice to use "return false;" on a listener, but I'm not even sure if what I have IS a listener, let alone whether it's bad to do. It seems to break me out of the loop, so it seems to work for me. The more obvious problem, however, is that the alert doesn't fire when the page is refreshed / loaded up. It works fine if someone changes the value thereafter though...

    My Questions:

    1.) If a space exists between words in the value attribute within markup of any of the radio buttons, should I encapsulate this in double-quotes when referencing it within the jQuery? (i.e. - if one of the radio buttons has "Group A" instead of "GroupA", should this be encapsulated like "Group A" or instead like 'Group A'--or maybe no quotes at all?)

    2.) How can I make this work regardless of state? (I.e. - at page-load and after page-load.)

    3.) Does the looping logic appear to be correct?

    Thanks in advance.

  2. #2
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured out how to get what I needed...

    1.) I'm using single quotes and it's working... Ap pro po...

    2.) Everything in my jQuery file is already encapsulated inside a $(document).ready(function(){} block, so page loading state should be accommodated for (i.e. - knowing how to handle how values appear in form elements when being retrieved from the database), which leaves user interaction and reaction (which I handled in an EACH block for each input).

    3.) The concept I wrote about on here was correct--the syntax, well, probably not so much. What I did was write straight forward IF blocks for whatever I needed to be handled from page loading but then for non-page loading-specific logic (aka, user manipulated values or interfaces), I put inside the EACH block.

    (Probably doesn't make sense unless you were messing with something like this but feel free to ask me any questions. The code could probably be cleaned up a bit and I'll post a better context later if anyone is interested.)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •