swap out the CSS class for my own class dynamically

When I display HTML code like this

<div class="btn btn-pagination">
   <i class="fa fa-angle-right"></i>
</div>

and want to replace fa fa-angle-right with myClass when the page loads.

I attempted:

$(document).ready(function () {
   $('.btn.btn-pagination').children($('.fa.fa-angle-right').removeClass());
   $('.btn.btn-pagination').children().addClass('myClass');
})

This approach did not work. Can you help me understand what I'm doing wrong here?

Answer №1

For a more efficient solution, it is recommended to access the DOM only once.

jQuery(document).ready(function($) {
  return $('.btn.btn-pagination .fa.fa-angle-right')
    .removeClass()
    .addClass('foobaz')
  ;
});
.foobaz { background: red; width: 200px; height: 100px; margin: 0 auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn btn-pagination">
  <div class="fa fa-angle-right"></div>
</div>

Answer №2

While your current code may work, an alternative method using a more efficient selector and jQuery chaining can be seen below:

Give this a try:-

$(document).ready(function () {
   $('.btn.btn-pagination').children('.fa.fa-angle-right').removeClass().addClass('myClass'); 
});

See Demo

OR

$(document).ready(function () {
   $('div.btn.btn-pagination > i.fa.fa-angle-right').removeClass().addClass('myClass'); 
});

Check out Demo

Answer №3

Web Development

<div class="btn btn-pagination">
   <i class="fa fa-angle-right"></i>
</div>

JavaScript

$(document).ready(function(){
    $(".fa .fa-angle-right").addClass('myClass');
     $(".fa .fa-angle-right").removeClass('.fa-angle-right');
    });

Answer №4

For a demonstration, click here.

Below is the code snippet:

HTML:

<div class="btn btn-pagination">
  <i class="fa fa-angle-right"></i>
</div>

JS:

$(document).ready(function() {
  $('.btn.btn-pagination > i').attr('class', 'myClass');
})

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

Is it necessary to send form data back with Express, or is there an alternative solution?

I am facing a simple problem with my handlers for /login get and post requests. Here is the code: loginRender(req, res) { let options = { title: 'Login', layout: 'auth.hbs' } res.render('login', options) } logi ...

Utilizing JavaScript: Storing the output of functions in variables

Currently in the process of learning JavaScript and aiming to grasp this concept. Analyzed the following code snippet: function multiNum(x, y){ return x * y; } var num = multiNum(3, 4); document.write(num); Decided to try it out on my own, here's ...

The pre-save function in Mongoose does not seem to be working properly when using discrimin

I am facing an issue where the pre save hook is not being called before saving the owner in mongoose. Is there a workaround for this problem? const baseOptions = { discriminatorKey: '__type', collection: 'users' } const Base = ...

Challenges with Incorporating Stripe

I am currently working on integrating Stripe into a school project. Despite following the documentation and instructions provided, I keep encountering a POST 500 (INTERNAL SERVER ERROR) along with an error message stating "Error fetching payment intent: Er ...

Discrepancy in post results

I am currently working on integrating two scripts - a Prestashop DHL label creator and our company's internal sales application. The goal is to streamline the process of generating DHL labels directly from our sales application without the need to acc ...

Attempting to create a slider utilizing jQuery

I'm currently working on creating a slider using jquery. I have downloaded the cycle plugin for the slider and included it in my file. The slider consists of 7 pictures. Below is the code I am using, can someone please help me identify any issues? &l ...

Transaction response for embedded iFrame with Accept.js on Authorize.net

I've recently integrated authorize.net accept.js embedded iFrame into my application, but I'm facing difficulties in setting the transaction response in my lambda function to retrieve the expected data. Although I've checked similar queries ...

Foundation framework enables the creation of a full width footer that stands out

I'm having trouble getting my footer panel to stretch across the entire page, it seems stuck in the center. The header at the top of the page spans the full width just fine. Am I missing something? My goal is to make both panels at the bottom of the p ...

What could be causing my update function to not run when I refresh the page?

As a non-developer trying to learn react.js, I am working on a section of my website that needs to update every few seconds with new content like a photo, header, and body text. I've encountered an issue where the data refreshes correctly after the p ...

What is the best way to delete the <li> item from a <ul> element using its Id?

I currently have the following HTML structure: <ul id="ulId"> <li id="firstli"> </li> <li id="secondli"> </li> </ul> and my goal is to remove the secondli element. After doing some research, I attempte ...

Creating an object and setting its prototype afterwards

While exploring examples and questions online about prototypal inheritance, I noticed that most of them involve assigning prototypes to constructor functions. One common pattern is shown in the code snippet below: Object.beget = function (o) { var F = ...

Conflict with iScroll

When using an HTML5 template in Mobile Safari on iPad, I encountered an issue with a div containing iScroll. Within this div, there is a jQuery function within an if/else statement which tests if the user is tapping outside of the iScroll div and fades it ...

Combine array in MongoDB containing nested documents

I need assistance with querying my MongoDB database: Specifically, I want to retrieve data that is nested within an array and filter it based on a specific key within the nested structure. The example document looks like this: [ { "name": ...

Having trouble getting a background image with tint to display properly in Bootstrap

I applied a sleek black transparent gradient to my body background picture, but as soon as I integrated bootstrap into the HTML code, the effect ceased to function. body { background: linear-gradient( rgba(0, 0, 0, 0.5), rgba ...

Ajax encounters difficulty in parsing JSON data

I have thoroughly researched similar questions on this topic, but none of them have provided a solution to my problem. My current challenge involves parsing JSON data that is being returned from a PHP script running on the server. I have already used JSON ...

The importance of adding ".$" to an AngularJS filter object

I have been exploring a section of code related to filtering in AngularJS from the documentation website. (cf. http://docs.angularjs.org/api/ng.filter:filter) Specifically, I am curious about the use of .$ appended to the object search, as shown in the fo ...

How come my div is taking on the height of something that isn't its direct ancestor?

I am in the process of creating a website that features a consistent design element of an inset border surrounding a block of content within a <div>. This inset border is achieved using LESS/CSS, and is contained within a separate <div class="inse ...

Retrieve the response status using a promise

There is a promise in my code that sometimes results in an error response (either 400 or 403, depending on the user). I am trying to handle this situation by catching the response and implementing a conditional logic to execute different functions based on ...

The image I want to show is invisible on the CSS custom radio button

I'm having trouble creating a custom radio button that displays the image I want. The current setup doesn't seem to be working as expected. label { font-weight: lighter; cursor:pointer; } input[type="radio"] { display:none; } in ...

Eliminate forward slashes from the telephone number

I am trying to figure out how to format a USA phone number by removing slashes and brackets. I have searched on Google, but I cannot find the answer. I would like to use regex to achieve this, while keeping the plus sign at the start intact. For example, ...