Home > Back-end >  Short hand for jquery selector ":checked" applied on a list
Short hand for jquery selector ":checked" applied on a list

Time:10-22

I have a list of jquery radio buttons :

<input type="radio" class="editorInput" name="radioEditor" value="1" id="radio_1">
<input type="radio"  name="radioEditor" value="2" id="radio_2">
<input type="radio"  name="radioEditor" value="3" id="radio_3">
<input type="radio"  name="radioEditor" value="4" id="radio_4">

I need to find the one that is checked, and get its value (or text).

For value I know I can do :

var val = $(".editorInput:checked").val();

But in some other cases, in my script I have an already build list containing these controls, for example selected by

var list = $(".editorInput");

The only way I know to get the checked one in this list requires to write an each iterator :

$(list).each(function ()
{
    if ($(this).is(":checked"))
    {
        val = $(this).val();
    }
});

My question is : is there a shorter jquery syntax to get the "checked" radio button from a list of controls (like above) containing several controls, in a single line ?

Thank you

CodePudding user response:

You can modify jQuery objects, for example one way to do it is [filter() Method][1]

Another honorably mentions should go to find() Method

$list.filter(':checked') filters out checked elements.

$ulList.find('li') finds all <li> elements inside the list.

  • Related