-2

If I just have this js everything fires and works:

$('.hidden-div').hide();

$('#id_bool').change(function() {
   $('.hidden-div').toggle(this.true);
});

but if I add this function directly underneath it everything stops working:

$('.form').on('submit', function(){
   if $('#id_bool'){
     $('.hidden-div').show();
   } else {
     $('.hidden-div').hide();
   }
});

What is going wrong in the second part?

  • 3
    what do you mean by "stops working"? Is there an error in the console? If not, what is it that behaves differently from how you would want? – Robin Zigmond Sep 29 '18 at 20:11
  • 2
    because your IF syntax is wrong you need to wrap in if() { ...and also make any condition $(#id_bool) is nothing. – daremachine Sep 29 '18 at 20:11
  • guys it helps when you down vote to explain why so that I can fix it or learn from it. –  Sep 29 '18 at 20:22
  • Chaning the question so that it has another problem should not be done. The [original question](https://stackoverflow.com/revisions/52572154/1) had: `if $('#id_bool') {` as problem which was answered correctly by the [answer of Omid Nikrah](https://stackoverflow.com/a/52572173/1960455). But because you have corrected that problem the anwer became incorrect. If you have a further problem, please create a new question. – t.niese Sep 29 '18 at 20:39

2 Answers2

0

You forget to add parentheses in your if statement:

$('.form').on('submit', function(){
   if ($('#id_bool')){
     $('.hidden-div').show();
   } else {
     $('.hidden-div').hide();
   }
});
Omid Nikrah
  • 2,444
  • 3
  • 15
  • 30
  • Ok thanks for the grammar lesson. Still not achieving what I intend to do, but will keep at it. –  Sep 29 '18 at 20:15
  • 1
    @Whodini why you pass `this.true` to `toggle` method? – Omid Nikrah Sep 29 '18 at 20:17
  • see this question: https://stackoverflow.com/questions/52570497/how-do-i-integrate-toggle-with-form-errors –  Sep 29 '18 at 20:20
  • I tried this.checked but I need a hidden field to show only if a boolean is true. Still doesn't work with this.true, but I'm experimenting... –  Sep 29 '18 at 20:21
  • 1
    A jQuery object always evaluates as truthy, so it would never hide. – Heretic Monkey Sep 29 '18 at 20:34
0

In JQuery a selector will always return a JQuery array, you need to specify a property to check. This:

   if ($('#id_bool')){

should be:

  if ($('#id_bool:checked').length){

Edit:

if your id_bool has a value, that can be either true or false you can, as @Bargi mentions, use this:

if ($('#id_bool').val()){
Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • Not really, that still produces a truthy jQuery collection. Either `$('#id_bool').is(':checked')` or `$('#id_bool').val()`. – Bergi Sep 29 '18 at 20:22
  • @Bergi How about to check if field is true as opposed to checked? –  Sep 29 '18 at 20:28
  • @Whodini I don't even know what kind of field it is, so I can't tell – Bergi Sep 29 '18 at 20:30
  • @Bergi it's a Django BooleanField with choices specified: BOOL_CHOICES = ( (True, 'Yes'), (False, 'No') ) and it has a radio select widget, so it renders as a radio yes or no. –  Sep 29 '18 at 20:33
  • @Whodini https://stackoverflow.com/questions/8622336/jquery-get-value-of-selected-radio-button – Bergi Sep 29 '18 at 20:50