0

I have 2 checkboxes:

<div>
    <input type="checkbox" id="dx_article_excerpt">
    <label for="dx_article_excerpt">
        <?php _e( 'Displayyyyyyyy Excerpt', 'dxeasypb' ); ?>
    </label>
</div>
<div>
    <input type="checkbox" id="dx_article_content">
    <label for="dx_article_content">
        <?php _e( 'Display Content', 'dxeasypb' ); ?>
    </label>
</div>

I want when the first is clicked, the content checkbox to be disabled automatically. I've tried the following thing:

if (document.getElementById('dx_article_excerpt').checked) {
    document.getElementById("dx_article_content").disabled = true;
}

But guess what.. it's not working.

1
  • use radio button it is made for this kind of function Commented Jun 28, 2017 at 9:31

3 Answers 3

1

Use click event to change the content check status as below snippet using jquery :

$(function() {
  $("#dx_article_excerpt").click(function(){
     $("#dx_article_content").prop("disabled",this.checked);
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><input type="checkbox" id="dx_article_excerpt"><label for="dx_article_excerpt">first</label></div>
<div><input type="checkbox" id="dx_article_content"><label for="dx_article_content">content</label></div>

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

Comments

1

$(".checkbox").change(function() {
  $(".checkbox").not(this).prop("disabled", $(this).is(":checked"))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><input type="checkbox" id="dx_article_excerpt" class="checkbox"><label for="dx_article_excerpt">1</label></div>
<div><input type="checkbox" id="dx_article_content" class="checkbox"><label for="dx_article_content">2</label></div>
1. Add a class for both checkbox. 2. Use the status of current checkbox changed to prop disabled of the other checkbox

1 Comment

@RumenPanchev did you add the class to both checkbox?
1

Why is not working?

  1. Because no event is there .assign onchange event

    onchange="change()"

  2. Wrap the js inside the function

Updated with toggle

function change(){
        document.getElementById("dx_article_content").disabled = document.getElementById('dx_article_excerpt').checked;
}
<div><input type="checkbox" id="dx_article_excerpt" onchange="change()"><label for="dx_article_excerpt"><?php _e( 'Displayyyyyyyy Excerpt', 'dxeasypb' ); ?></label></div>
<div><input type="checkbox" id="dx_article_content"><label for="dx_article_content"><?php _e( 'Display Content', 'dxeasypb' ); ?>

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.