Welcome Guest, Not a member yet? Register   Sign In
hide/show element using jQuery
#1

[eluser]fcodebue[/eluser]
Into my view I need to hide/show elements using jQuery if some one choose one value of a radio button.

I load coorectly into my view header my jQuery library 1.5.1 and I wrote something like this

function imposta_rspp() {
if $('#rspp_interno_esterno'). == 'I' ) {
jQuery('#rspp_nome').show()();
jQuery('#rspp_formazione').show()();
jQuery('#rspp_data_ultima_formazione').show();
else {
jQuery('#rspp_nome').hide();
jQuery('#rspp_formazione').hide();
jQuery('#rspp_data_ultima_formazione').hide();
}
}

naturally I create my radio button with this facility


$rspp_i= array(
'name' => 'rspp_interno_esterno',
'id' => 'rspp_interno_esterno',
'value' => 'I',
'checked' => $checked,
'onclick' => 'imposta_rspp()'
);
$rspp_e= array(
'name' => 'rspp_interno_esterno',
'id' => 'rspp_interno_esterno',
'value' => 'E',
'checked' => !$checked,
'onclick' => 'imposta_rspp()'
);
echo 'interno'.form_radio($rspp_i).'esterno'.form_radio($rspp_e);


someidea why doesn't work
#2

[eluser]Davide Bellini[/eluser]
This syntax is wrong...

Code:
if $(’#rspp_interno_esterno’). == ‘I’ ) {

Try with :

Code:
if ($('#rspp_interno_esterno').val() == 'I' ) {

I can suggest you using Firebug Console with Firefox to check others syntax errors in javascript.

Cheers
#3

[eluser]InsiteFX[/eluser]
You can also save yourself some typing by doing this in your jQuery functions.
Code:
(function($) {

})(jQuery);
Now you do not have type jQuery on every line!

InsiteFX
#4

[eluser]danmontgomery[/eluser]
You shouldn't have more than one element with the same id attribute. This will cause problems for you with javascript.

Also, as InsiteFX said, there's much better shorthand for jQuery.

Code:
$(function() {

});

// or

$(document).ready(function(){
  
});

For checkboxes, you should use :checked. This looks something like:

Code:
$(function() {
    $('#rspp_interno_esterno').change( function() {
        if($(this).is(':checked')) {
            $('#rspp_nome, #rspp_formazione, #rspp_data_ultima_formazione').show();
        } else {
            $('#rspp_nome, #rspp_formazione, #rspp_data_ultima_formazione').hide();
        }
    });
});

This is all covered pretty thoroughly in the jQuery documentation and jQuery forums.




Theme © iAndrew 2016 - Forum software by © MyBB