# Get Category Name using JS

**URL:** <https://meta.discourse.org/t/get-category-name-using-js/177110>\
**Category:** Development\
**Created:** [January 24, 2021, 1:55pm UTC](https://meta.discourse.org/t/get-category-name-using-js/177110 "2021-01-24T13:55:46Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)\
**Post date:** [January 25, 2021, 11:57pm UTC](https://meta.discourse.org/t/get-category-name-using-js/177110/2 "2021-01-25T23:57:17Z")

</div>

There might be a way to consistently do it across both topics and category pages, but I’ve gotten the category ID in two different ways… one for category pages:

```xml
<script type="text/discourse-plugin" version="0.8">
const container = Discourse. __container__ ;
const controller = container.lookup('controller:navigation/category');

api.onPageChange((url, title) => {
  console.log(controller.get("category.id"));
});
</script>

```

and a similar way for topic pages:

```xml
<script type="text/discourse-plugin" version="0.8">
const container = Discourse. __container__ ;
const controller = container.lookup('controller:topic');

api.onPageChange((url, title) => {
  console.log(controller.get("model.category_id"));
});
</script>

```

In the category page example you can alternatively use `category.name` or `category.slug` but with the topic model only the `category_id` is available so you’d have to do a little more work there to get the name. ID might be a better path anyway because it’s consistent even if the category name or slug changes.

---

_[View the full topic](https://meta.discourse.org/t/get-category-name-using-js/177110)._
