jQuery bug in view. Trying to show/hide a textbox based on a checkbox being checked.

370 views
Skip to first unread message

Jordan Ladora

unread,
Jun 21, 2013, 5:07:05 PM6/21/13
to web...@googlegroups.com
Hi,

I have a jQuery script in a view. It has a bug as it's not working (textbox always shown). I think my syntax in the if statement is wrong.. here it is-

<script>

jQuery(document).ready(
  function()
  {
    if( jQuery('#cnd').prop('checked', true) );
    {
      jQuery('#nnd').show();
    }
    else
    {
      jQuery('#nnd').hide();
    }

  }
  );      

</script>


The element with id #cnd is a checkbox, and the element #nnd is a textbox. Putting a single line inside the doc ready function, eg

jQuery('#nnd').show();

hides or shows the box just fine.

Thanks for any help!!!

-jl

Jordan Ladora

unread,
Jul 25, 2013, 3:05:36 PM7/25/13
to web...@googlegroups.com
Hi,

I'm still stuck here.. I tried a few other things. I just want to show/hide #nnd based on whether or not #cnd is checked. This example is in the manual, but I still cannot get it to work..

Here's the form-

   fields = []
    fields.append( Field( 'cnd', 'boolean', default=True) )
    fields.append( Field( 'nnd') )

    form = SQLFORM.factory(*fields)


And the view-

<script>

jQuery(document).ready(function()
  {
    jQuery('#no_table_nnd__row').hide();
    jQuery('#no_table_cnd').change(function(){
      if( jQuery('#no_table_cnd').attr('checked') );
        jQuery('#no_table_nnd__row').show();
      else
        jQuery('#no_table_nnd__row').hide();});
  });     

</script>


But still the input box stays visible regardless of the status of the checkbox.. :( Any help would be greatly appreciated.

Thanks,
-j




-jl

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/EqxNV-LO7oY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Paolo Caruccio

unread,
Jul 25, 2013, 3:49:13 PM7/25/13
to
Maybe related with https://groups.google.com/d/msg/web2py/om58tz-DVOk/Lt5beXcLiH8J

By resuming the api:

 As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

Therefore, you should replace .attr() with .prop() if you are using a recent version of jQuery  because, in the context, "checked" is a property value and not an attribute.

Jordan Ladora

unread,
Jul 25, 2013, 6:52:01 PM7/25/13
to web...@googlegroups.com
Hi Paolo,

Thanks, but no luck.. I replaced


      if( jQuery('#no_table_cnd').attr('checked') );

with

      if( jQuery('#no_table_cnd').prop('checked') );

..but the box is still displayed by default & doesn't change when I check/uncheck the checkbox..

Cheers,
-j



On Thu, Jul 25, 2013 at 12:39 PM, Paolo Caruccio <paolo.ca...@gmail.com> wrote:
Maybe related with https://groups.google.com/d/msg/web2py/om58tz-DVOk/Lt5beXcLiH8J

By resuming the api:

 As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

Therefore, you should replace .attr() with .prop() if you are using a recent version of jQuery 

Il giorno giovedì 25 luglio 2013 21:05:36 UTC+2, Jordan Ladora ha scritto:

Paolo Caruccio

unread,
Jul 25, 2013, 7:21:13 PM7/25/13
to web...@googlegroups.com
I double checked your script and I discovered some errors.

Please try this:

<script>
jQuery
(document).ready(function() {
    jQuery
('#no_table_cnd').change(function(){
       
if( jQuery('#no_table_cnd').prop('checked') ){

            jQuery
('#no_table_nnd__row').show();
       
}else{
            jQuery
('#no_table_nnd__row').hide();};
   
});
});
</script>

Jordan Ladora

unread,
Jul 25, 2013, 8:10:29 PM7/25/13
to web...@googlegroups.com
Perfect - thanks for your help. I was sloppy with the javascript..

Best,
-j
Reply all
Reply to author
Forward
0 new messages