1

so lately i have been working on a little bit of js.

so, basically, my problem is that i need to hide whatever is passed in the parameters or show it if it already hidden.

Here is my code:

<script type='text/javascript'>
<!--
function toggleReport(table){
//the table argument is the table's id
alert(table);                    //to check that the name of the table is right
if($('#table').is(':visible')){ //check if visible
    $('#table').hide();     //if so, hide it
    alert('hide');          //send a message that it is now being hidden
}else{                          //if already hidden
alert('show');                  //send a message that it is now being shown
$('#table').show();             //show the table
}

}
//-->
</script>

however, it doesn't work.... it is sending the alerts, and everything is correct, however, it does not hide or show the table....

but, if i try doing this:

<script type='text/javascript'>
<!--
function toggleReport(){
//removed the argument
alert('table_1');
if($('#table_1').is(':visible')){
    $('#table_1').hide();
    alert('hide');
}else{
alert('show');
$('#table_1').show();
}

}
//-->
</script>

It works! why is it like that? because im going to have many tables on the website and other things that need to be hidden and shown and i dont want to make a new function for each.. :/

Please help me!

THANKS!!

6 Answers 6

2

Very Simple use toggle() intead of show()/hide(), toggle() makes element visible if it is hide and hide it if it is visible.

<script type='text/javascript'>;
function toggleReport(element_ID){
$("#"+element_ID).toggle();
}
</script>

If you want to hard code the Element ID than use following script

<script type='text/javascript'>
function toggleReport(){
$("#table_1").toggle();
}
</script>

Cheers, and dont forgot to vote up my answer :)

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

Comments

1

There's a prebuilt Jquery function for this.

Comments

1

If you're passing an element reference in, use that as your selector:

function toggleReport(table){
    $(table).toggle();
}

Note I'm using .toggle(), which will do exactly what you're attempting to do manually. If you wanted to log the new state, you can do so in the callback:

function toggleReport( table ) {
    $( table ).toggle('fast', function(){
        console.log( "Element is visible? " + $(this).is(":visible") );
    });
}

Note, if you're passing in the ID of the element, your selector will need to be modified:

$( "#" + table ).toggle();

3 Comments

thanks, but what do i pass in the parameters of toggle? my table is from a class that i made in css called 'hidden', and the display is equal to none over there, now how do i make toggle to toggle it on and off? and sorry if my question sounds stupid.. im pretty new to js..
@Baruch jQuery will handle that for you - don't worry about the CSS. Just call $(table).toggle(), where table is the reference to your element.
@Baruch in the example provided here you should be passing a reference to the DOM element you wish to show/hide. It could be "raw" or an already selected jQuery object.
0

You can use toggle function to achieve this....

$(selector).toggle();

Comments

0

demo http://jsfiddle.net/uW3cN/2/

demo with parameter passed http://jsfiddle.net/uW3cN/3/

Good read API toggle: http://api.jquery.com/toggle/

code

function toggleReport(){
//removed the argument
$('#table_1').toggle();

}

another sample rest html is in here http://jsfiddle.net/uW3cN/3/

function toggleReport(tableid){
//removed the argument
$('#'+tableid).toggle();

}

Comments

0

The mistake you made in your original function is that you did not actually use the parameter you passed into your function.You selected "#table" which is simply selecting a element on the page with the id "table". It didn't reference your variable at all.

If your intention was to pass an ID into the selector should be written, jQuery("#" + table). If table is instead a reference to a DOM element, you would write jQuery(table) (no quotes and no pound sign).

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.