César
César

Reputation: 27

select the last radio button of the group

I have several groups of radiobuttons in a form but I wonder if the latter is selected radiobutton in a specific group, I tried this:

$('#myform').submit(function() {

if ($('input[name$="cats"]:last', this).is(':checked')) {

            alert ('the last button');
            return false;

}

});

It does not work... I hope your help, thank you and sorry for my english xd

EDIT...

strange, I have no error in the error console or firebug.

My form is broad, but this is an example:

<form id="myform" action="">

<fieldset class="left">
<legend>My Dogs</legend>                        
<p><input name="dogs" type="radio" value="Doberman" class="radio" /> Bobby</p>
<p><input name="dogs" type="radio" value="German Shepherd" /> Drake</p>
<p><input name="dogs" type="radio" value="schnauzer" /> Bunchie</p>
<p><input name="dogs" type="radio" value="Others" /> Other</p>
<p><input name="otherdog" id="otherdog" type="text" class="text" /></p>
</fieldset>

<fieldset class="right">
<legend>My Cats</legend>
<p><input name="cats" type="radio" value="Balinese" /> Kitty</p>
<p><input name="cats" type="radio" value="Bengal" /> Chucky</p>
<p><input name="cats" type="radio" value="Chausie" /> Gordon</p>
<p><input name="cats" type="radio" value="Others" /> Other</p>
<p><input name="othercat" id="othercat" type="text" class="text" /></p>
</fieldset>

</form>

And my JS is:

$(document).ready(function() {

$('#myform').submit(function() {

if ($(this).find('input[name$="dogs"]:checked').length == 0) {
            alert('Select dogs.');
            return false;
        }

        if ($('input[name$="dogs"]:last', this).is(':checked')) {

            alert ('this is the last');
            return false;
        }

});

if ($(this).find('input[name$="cats"]:checked').length == 0) {
            alert('Select cats.');
            return false;
        }

        if ($('input[name$="cats"]:last', this).is(':checked')) {

            alert ('this is the last');
            return false;
        }

alert('all good :)');
        return false;

});

});

the only thing that happens is that you do not enter the condition:

" if ($('input[name$="cats"]:last', this).is(':checked')) { "

Upvotes: 1

Views: 5278

Answers (2)

Kishore
Kishore

Reputation: 1912

What error are you getting?

i recreated this and it seems to work click here

Upvotes: 0

karim79
karim79

Reputation: 342635

Works for me, am I missing something here?

$('#myform').submit(function() {
   if ($('input[name$="cats"]:last', this).is(':checked')) {
        alert('the last button');
        return false;
   }
});

<form id="myform">
    <input type="radio" name="foocats"/>
    <input type="radio" name="foocats"/>
    <input type="radio" name="foocats" checked="checked"/>
    <input type="submit"/>
</form>

Upvotes: 4

Related Questions