samedi 19 mars 2016

How to update ("check") a specific checkbox in jquery without nesting the find? [duplicate]

This question already has an answer here:

Finding the element is one thing, updating a particularly indexed one is the question here.

Given basic HTML for a group of three checkboxes such as

<html>
<head>
<script src="http://ift.tt/1V0HKB6"></script>
</head>
<body>
Test!!!!!
<input name='cars' type='checkbox' value="primary" checked/>
<input name='cars' type='checkbox' value="secondary"/>
<input name='cars' type='checkbox' value="tertiary"/>
</body>
</html>

I can use jquery to select all the checkboxes with

$("input").prop( "checked", true );

However, how can I target just one checkbox such as the middle one?

I tried

$("input")[1].prop( "checked", true );

But I get prop is not a function

and $("input")[1].prop( "checked", true ); returns [] with no change.

It looks like I found one option which is a nested find, e.g.

$($("input")[1]).prop( "checked", true );

Is there a cleaner way than that?




Aucun commentaire:

Enregistrer un commentaire