0

How can I select the "container" div if the input checkbox inside "myCheck" is checked?
I am using the jquery print area plugin, so basically if checkbox is checked, I need to select the container div so the code should look something like $(".myCheck input:checked > div").printArea();

Here is my HTML:

<div class="content">
        <div class = "myCheck"><input type="checkbox" />
            <div class = "container"> 
                <img src="https://www.kent.ac.uk/graduateschool/images/email-thumbnail.jpg" alt="My new Image">
            </div>
            </div>
        </div>

I have tried using the > selector but it does not seem to work:

alert( $(".myCheck input:checked > div").printArea()

Also tried this:

alert( $(".myCheck input:checked").children()).printArea()
6
  • 1
    what do you mean by everything? Commented Feb 9, 2017 at 15:13
  • Are you looking for an event listener on the checkbox to run the function or are you expecting to just check the state of the checkbox when the script runs? Commented Feb 9, 2017 at 15:13
  • 1
    Also the input element does not have children, so input > div is incorrect as a selector. Commented Feb 9, 2017 at 15:14
  • by adding the input in the selector, you get it, not the div, you need to go back to parent() or any other structure relation function Commented Feb 9, 2017 at 15:16
  • I am using the jquery print area plugin, so basically if checkbox is checked, I need to select the container div so the code should look something like $(".myCheck input:checked > div").printArea(); Commented Feb 9, 2017 at 15:20

3 Answers 3

2

You could use .next()

$('input').on('change', function(){
  console.log($(".myCheck input:checked").next().html());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">
  <div class = "myCheck"><input type="checkbox" />
    <div class = "container"> 
      <img src="//placehold.it/50" alt="My new Image">
    </div>
  </div>
</div>

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

3 Comments

would look cleaner if target specific input
@charlietfl You're very correct. I could use $(this).filter(':checked').next(), but because the event listener is only for the demo, I can't be sure the input will be the context of where OP wants to execute the code. This answer just illustrates that OP should use the .next() method to select a later occurring adjacent sibling.
right but conversely would also be triggered on every input in page. Just making a note so OP is aware
0

Using jQuery you can use this for the checked value:

$('.myCheck').prop('checked', true);

And you can probably use a callback function to select the container :

$('.myCheckbox').is(':checked', function(e){
  e.preventDefault(e);
  $('container').theMethodYouWantToUse
});

Hope it help you

Comments

0

Run the example below, check and uncheck the component, hope it helps!

Basically, I'm using simple callback for checkbox's clicks, controlling it's state, and selecting the content if the component is checked.

// Waiting for DOM load
$(document).ready(function() {

  // Callback for checkbox's click
  $(".myCheck > input[type='checkbox']").on("click", function() {

    // Setting the container variable as null
    var container = null;
    
    // Checking if the checkbox is "checked"
    if ($(this).is(":checked")) {

      // Selecting the container node object, so you can use it as you want
      var container = $(".container");
      
      // Console warnings
      console.info("Container selected, the checkbox must be checked!");
      console.log("Some test information from container: " + $(container).find('span').html());

    } else {

      // Console warnings
      console.info("Container not selected, the checkbox must be checked!"); 
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">
    <div class="myCheck">
        <input type="checkbox" />
        <div class = "container"> 
            <span>Some example content..</span>
        </div>
    </div>
</div>

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.