1

Hoping someone could help me with a bit of jquery or javascript. I have some DIV's that contain the values of a checkbox being either "1" or "0" depending on whether I check the box or not:

<div class="checkbox">1</div>   //This is when the checkbox is checked

<div class="checkbox">0</div>   //This is when the checkbox is NOT checked

The class for this DIV stays the same whether it is a 0 or a 1 so I need to have a conditional statement that says,

"If the contents of the DIV is 1 then show it"

AND

"If the contents of the DIV is 0, then hide it"

Would this be simple to do?

1
  • It would be better to give another class to each element, like "when-checked" for one and "when-not-checked" for the other. Having to look in the DOM at the contents is messy and slower (though performance probably won't be a real problem here). Commented Jan 15, 2013 at 17:46

5 Answers 5

3

A filter would come in handy for such case..

$('.checkbox').filter(function () {
   return $(this).text() == 0;
}).hide();
Sign up to request clarification or add additional context in comments.

7 Comments

What if it changes after this filter runs
Put it inside a change listener like my answer. I'd combine mine and Vega's answers (though his deserves the Answer)
@idrumgood, disagree. Yours doesn't run unless an event has been triggered
This works great thanks guys, I am noticing that the "0" shows up just briefly before the script runs and then it disappears. Is there anyway to stop that? I have the script at the bottom of the page...it doesn't work when placed at the top.
If you need it to run on page load AND when checkboxes change, then you're going to want to take this filter code, put it in a function, then call that function both on page load and on a change listener.
|
2

I would do it differently.

$("input#checkbox").change(function(){
  $("div.checkbox").toggle(this.checked);
});

Considering that your checkbox is the one that it is altering the content of the <div> anyways.

2 Comments

I think he want to show the div.checkbox with text content 1 and hide div.checkbox with text content 0
I agree with your answer in that regard and you were faster than me though
0

Any time a checkbox is changed, look at your divs with class checkbox and if they have 1, show, else hide.

$('input[type="checkbox"]').on('change', function() {
    $('.checkbox').each(function(){
        if($(this).text() === '1'){
            $(this).show();
        }else{
            $(this).hide();
        }
    });
});

1 Comment

While mine works, @Vega 's answer is much cleaner and does the same thing as my .each() function.
0

If i have to do it then i would love to do in this way: http://jsfiddle.net/P2WmG/

var chktxt = $.trim($('.checkbox').text());
if (chktxt == 0) {
   $('#checkbox').hide();
} else {
   $('#checkbox').show();
}

Comments

-1

You can use the :contains() Selector to select the divs based on their contents.

$('div.checkbox:contains("1")').show();
$('div.checkbox:contains("0")').hide();

2 Comments

Contains should be used with extreme care to prevent false-positives
Agreed, but Dan specified that the contents are only either "1" or "0" so you'd be hard pressed to get false positives.

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.