JQuery hide Class if other class is visible or shown

Found similar questions but nothing that covers exactly what I need. I kept it simple in my example and I want to use JQuery.

I have two classes. I want to hide the "filter" div if the "category" div is shown on page load. There are currently NO styles associated with either class. I believe I am pretty close but it doesn't work.

<script language="text/javascript">
if(!$(this).hasClass("category")){
$('filter').css('display', 'none');
}
</script>

<div class="category">By Category</div>
<div class="filter">By Custom Filter</div>

Thanks in advance!

Answers


Use .length to test if an element is present.

Use .hide() & .show() to show and hide elements.

And finally, you want the code to run only when the page has finished loading, so you want to wrap it all in $(document).ready().

So something like this should work best:

<script language="text/javascript">
    $(document).ready(function() {
        //following code will hide all elements with a class of 'filter'
        //if any elements with a class of 'category' are found
        if($('.category').length){
            $('.filter').hide();
        }
    });
</script>

<div class="category">By Category</div>
<div class="filter">By Custom Filter</div>

HTHs, Charles


Need Your Help

PostgreSQL IF EXITS inside a FUNCTION Error 42601

function postgresql if-statement syntax-error

I want to create a function that will return an integer variable (or boolean) after check if there are any row in table RESERVATION have an RESERVATION_ID that equal to the reser_id I give to it. B...

cmd terminates when delete fails

windows-7 batch-file

I have the following bat file mybat.bat that:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.