1

Given some markup where there are a series of inputs (checkboxes) at an arbitrary depth, how can I determine if a given input is checked based on its value:

<ul id="root_node">
  ...
  <li>
    ...
    <span>
      <input value="val_1" ... />
      ...
      <input value="val_2" ... />
      ...

So, what I need is: given root_node and an input value (e.g. val_2), I want to determine if the corresponding checkbox (somewhere underneath root_node) is checked.

2 Answers 2

2

You can jQuery selections based on attributes: http://api.jquery.com/attribute-equals-selector and the :checked 'pseudo class'

$('input[value="val_1"]:checked')

so you could do:

if $('input[value="val_1"]:checked').val() !== undefined) {
  // do something
}

Hope this help,

Martin

Sign up to request clarification or add additional context in comments.

Comments

1

You can do something like:

var context = "root_node";
var value = "val_2";

var checked = $("input:checkbox[value='" + value + "']",
    $("#" + context)).attr("checked");

If the context never changes, you can shorten the above into:

var checked = $("#root_node input:checkbox[value='" + value + "']")
    .attr("checked");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.