What is the best way to implement a toggle feature for my icon using jQuery and MaterializeCSS?

When using MaterializeCSS, there is a specific syntax for utilizing their premade icons. Take a look at this example:

<i class="material-icons">expand_more</i>

The framework recognizes the desired icon based on the text within the i tags. I am currently working on creating a collapsible division with jQuery, and I want the icon to switch between an upward and downward arrow. To achieve this, I need to modify the text inside the i tags.

I have managed to change it once successfully, but I am facing difficulty reverting it back when collapsing the div. Here is the code snippet:

$('.remove-text').click(function() {
  $(this).closest('.card').toggleClass('collapsed');
  if ($('.arrow-change').text = 'expand_more') {
    $('.arrow-change').text('expand_less');
  } else {
    $('.arrow-change').text('expand_more');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="remove-text">
  <i class="material-icons arrow-change">expand_more</i>
</div>

Answer №1

When you use =, you are assigning a value, but what you really need is == or === for comparison.

The double equals does not require strict type matching, whereas the triple equals does.

Additionally, with jQuery it should be text() instead of just text. For vanilla JavaScript, you would use innerText.

Answer №2

Give this slideToggle() a try!

$( "#expandableSection" ).slideToggle( "slow", function() {
// Transition complete.

});

Check out my example on Plunker

Answer №3

Give this a try:

$('.hide-content').click(function() {

var element = $(this);

element.toggleClass('collapsed');

if(element.hasClass('collapsed')){
    element.children('i.arrow-change').text('expand_less');
}else{
    element.children('i.arrow-change').text('expand_more');
}

});

Check out an example here: https://jsfiddle.net/t8182mca/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem with top navigation not behaving correctly when set as fixed at the top

I encountered a strange issue with the sticky top navigation bar on my website. You can view the problem by visiting this link to the fiddle code and I've included the relevant details below. There are three specific areas where the problem arises: ...

What is causing this array to automatically alter its value?

How can I prevent A from changing its value when I only want to modify Q in the first loop of this html/javascript code? Why does A change even though I am only changing Q? What steps can I take to avoid A from changing? <!DOCTYPE html> <html& ...

Unable to perform the GET request method due to data indicating [object object]

Currently, I am attempting to use a get request method within both Vue and Express in order to retrieve data based on my v-model. Displayed below is the code where I am trying to send the data to Express. getResult() { axios .get( `${process. ...

requiring a page reload for an AJAX-loaded webpage

Encountering an issue with a third-party integration on a website specifically designed for iPads, where multiple pages are loaded using AJAX. Upon visiting the page for the first time, the expected functionality is missing and only appears after refreshi ...

Adjust the value of a select menu by utilizing a click event handler

I encountered a scenario where I am able to change the value of a select menu using a click function, however, the attached change event does not execute when the select menu value is changed from outside sources. It's puzzling because changing the va ...

Enter the ISO code for the country in an HTML form to submit

I'm currently working on a form that requires users to select a country from a list. I came across a website that provides a comprehensive list of countries in HTML format. However, the site displays the full country name but sends the ISO code to the ...

Troubleshooting the Nextjs-blog tutorial loading issue on localhost:3000

Looking to delve into Nextjs, I decided to start by following a tutorial. However, every time I attempt to run 'npm run dev', the local host just keeps loading endlessly. Upon inspecting and checking the console, there is no feedback whatsoever. ...

What is the best way to transform a string into a sequence of numbers using JavaScript?

Is there a clever method for transforming a string into a sequence of numbers in Javascript (not just converting "0.5" to 0.5, but rather "Hello" into 47392048)? Any suggestions are welcomed. Many thanks! ...

Setting up a Webpack configuration to compile modules within the node_modules directory

My webpack and babel configuration is causing some issues. I installed my component repository (es6 module without webpack configuration) as a node_module, but it's not working - I'm getting an 'Unexpected token import' error because Ba ...

converting an array (list) to Json format in C#

I have a method that looks like this: [HttpPost] [ActionName("GetUserData")] public ActionResult GetUserData() { using (var ctx = new myEntities()) { ctx.Configuration.LazyLoadingEnabled = false; ctx.Configuration.ProxyCreationEnab ...

The recharts error message displays: "There is no overload that matches this call."

I am currently working on developing a chart in react using the recharts library. To guide me, I am referencing an example provided in their documentation available at this link: https://codesandbox.io/s/zen-ellis-30cdb?file=/src/App.tsx While the project ...

Using ReactJS and Redux to dispatch notifications

I have a query regarding displaying notifications for successful or failed user registration actions. Utilizing Redux, I've implemented the following action page: export function registerUserFail(error){ return { type:REGISTER_USER_FAIL, ...

Run a Javascript function two seconds after initiating

My goal is to implement a delay in JavaScript using either setInterval or setTimeout, but I am facing an issue where the primary element is getting removed. Code that works fine without Javascript delay: const selectAllWithAttributeAdStatus = document. ...

Update a div in PHP using the data received from an AJAX response

I recently developed a PHP application and encountered an issue with updating the value of a date picker. The user is prompted for confirmation when changing the date, and upon confirmation, the date in the "expiry" field (with id expiry) should update alo ...

Arranging div blocks in columns that are seamlessly aligned

I am dealing with a situation where I have several boxes or blocks arranged in a 3-column layout. These boxes are styled using properties like float: left, width: 33%, but they have varying heights. Is there a way to make sure that the boxes always fill i ...

The issue with the max-height transition not functioning properly arises when there are dynamic changes to the max-height

document.querySelectorAll('.sidebarCategory').forEach(el =>{ el.addEventListener('click', e =>{ let sub = el.nextElementSibling if(sub.style.maxHeight){ el.classList.remove('opened&apos ...

What are some ways to broaden the list and select values that are constantly changing?

I am currently trying to find a solution for extracting the target of Add New from Prod2 in the provided code snippet. In order to retrieve this target, I need to first click on ptv-icon ptv-collapse-icon. The challenge lies in the fact that the XPath cha ...

Please ensure that there is a minimum of one checkbox with nested names

Update: After some troubleshooting, I discovered that my additional method was not being triggered due to calling addClassRules for my form class. To work around this issue, I included a hidden form field with no value and applied the custom validation to ...

The process of incorporating unsigned right shift for BigInt in JavaScript

I attempted the implementation provided in this answer, but unfortunately, it seems to be ineffective. function urs32(n, amount) { const mask = (1 << (32 - amount)) - 1 return (n >> amount) & mask } function flip32(n) { const mask ...

Placing a Div at One Third of the Height of the Viewport

Hello, I'm looking to enhance the appearance of my website using the rule of thirds. I have a tall div that I would like to position one-third of the way down from the top of the browser window viewport height. This is what I have tried so far, but ...