1

I am making a add or remove class functionality in JavaScript but it is not working.

here is my code...

 if (window.location.hash == "#/profile/"){
        document.getElementById("tabMenu").removeClass("bottomTabs").addClass("hidden");
    } else{
        document.getElementById("tabMenu").className += "show";
    };
2
  • I suspect you are trying to use JQuery.removeClass. I don't think angularJS have its own helpers to do so. Or you are writing your own? (than you need to post code how you wire it up to all elements). Commented Apr 9, 2014 at 5:39
  • @AlexeiLevenkov, AngularJS has a subset of jquery dom manipulation interface through angular.element(). This includes css class manipulators as well Commented Apr 9, 2014 at 7:08

5 Answers 5

2

If you are using AngularJS, then use the angular.element("#elementID") function to activate its internal jqlite so your code would look like this.

var tabMenu = angular.element("#tabMenu");
if (window.location.hash == "#/profile/"){
        tabMenu.addClass("hidden")
    } else{
        tabMenu.removeClass("hidden")
    };

Click here for angular.element documentation

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

3 Comments

(Same thing with this code )----is working but first time page load it is not after reload page it working and if i am load page in different window same thing doing after reload it working fine how to fix it.
imho, this has something to do with your thought process. use the hasClass to check for the currently containing class and switch classes appropriately using the add and removeClass method. My example was just trying to illustrate that you can do DOM class manipulations in angularjs.
+1. I did not know that andgularJS have its own implementation for basic operations.
1

If you want some simple and general purpose functions in plain javascript for adding and removing classes that won't disturb any other classes on the element that you can use in lots of places, you can use these:

function removeClass(elem, cls) {
    var str = " " + elem.className + " ";
    elem.className = str.replace(" " + cls + " ", " ").replace(/^\s+|\s+$/g, "");
}

function addClass(elem, cls) {
    elem.className += (" " + cls);
}


 var elem = document.getElementById("tabMenu");
 if (window.location.hash == "#/profile/"){
        removeClass(elem, "bottomTabs");
        addClass(elem, "hidden");
    } else {
        addClass(elem, "show");
 }

Comments

0

Using pure JS, it would look like below:

var myElement = document.getElementById("tabMenu");
if (window.location.hash == "#/profile/"){
    myElement.className = myElement.className.replace("bottomTabs", "");
    myElement.className = myElement.className + " hidden";
} else{
    myElement.className = myElement.className + " show";
};

See the demo below:

JSFiddle

Comments

0

Since you are using angular, why not let ng-class do the adding and removing of classes?

<div ng-class="{'bottomTabs': !onProfile(), 'hidden': onProfile(), 'show': !onProfile()}">
</div>

You just need to create a function that returns true when the profile page is in view:

$scope.onProfile = function() {
   return window.location.hash == "#/profile";
};

Here is a working demo: http://plnkr.co/xh3KkxaHKuLtqIPpfE5Z

Comments

0
<script type="text/javascript">
$(document).ready(function () {
    if (window.location.hash == "#/profile/") {
        $("#tabMenu").hide();
    }
}
</script>

Try this code. Before this add jquery in your page

5 Comments

removeClass() is a jQuery method, not javascript.
code is working but first time page load it is not after reload page it working and if i am load page in different window same thing doing after reload it working fine how to fix it.
when you are calling this code. Means in what event?
@Deepak2221 i am create bottomTabs for some unique pages but tabs are coming on all pages so i want to hide this on some page.
@user3256161 then you should accept the answer and upvote this.

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.