Update the CSS badge content following an AJAX request

In the webpage I've designed, there are navigation tabs with notification badges. When a tab is in focus, an ajax call is made to the server to reset the badge count to zero upon a successful response. However, despite displaying the badge and receiving the successful response from the ajax call (not included below), the badge value does not update as expected.

Here's the relevant code snippet:

<div class="container-fluid">
    <br/>
    <div class="col-sm-10">
        <div class="panel with-nav-tabs panel-tab-block">
            <div class="panel-heading">
                <ul class="nav nav-tabs">
                    <li class="active">
                        <a data-toggle="tab" class="" href="#projects-tab">Projects</a>
                    </li>
                    <li>
                        <a data-toggle="tab" id="forum-notification-badge" class="notification-badge" 
                        data-notification-badge="1" href="#forum-tab">Forum</a>
                    </li>
                </ul>
            </div>
            <div class="panel-body">
                <div class="tab-content">
                    <div id="projects-tab" class="tab-pane fade in active">
                        <div class="row">

                                <div class="col-sm-12">
                                    <div class="panel with-nav-tabs panel-tab-block">
                                        <h1>
                                            Projects
                                        </h1>

                                    </div>
                                </div>
                        </div>
                        <div class="row">
                            <div class="spacer-sml"></div>
                        </div>
                        <div class="row">
                            <div class="spacer-sml"></div>
                        </div>
                    </div>
                    <div id="forum-tab" class="tab-pane fade">
                        <div class="tab-block-content forum-posts">
                            <div class="">
                                <div class="col-sm-12">
                                    <h1>
                                        Forum
                                    </h1>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

JQuery Functionality:

jQuery(document).ready(function( $ ) {

    $('#forum-notification-badge').on("focus", function() {
        var self = $(this);

        // Perform ajax call and reset badge if successful
        self.data("data-notification-badge","0");
        console.log("Badge count has been reset to zero");

    });

});

CSS for Styling:

.nav {
   /* CSS Styles */
}

/* More CSS styles... */

.notification-badge {
    /* Positioning and styling for notification badges */
}

You can view this functionality in action via this jsfiddle link.

Answer №1

Try using attr() in place of the current code.

$('#notifications').on("click", function() {
    var element = $(this);

    //Perform an ajax request and reset the notification count if successful
    element.attr("data-notification-count",0);
    console.log("Notification count reset to zero");

});

Answer №2

When using the .data() method in jQuery, it only updates an internal variable stored by jQuery and not the actual HTML content. To update the HTML itself, you should utilize the .attr() method instead.

Check out this code snippet for a demonstration:

jQuery(document).ready(function($) {

  $('#forum-notification-badge').on("focus", function() {
    var self = $(this);

    // Perform ajax call and reset badge to zero if successful
    self.attr("data-notification-badge", "0");
    console.log("Badge set to zero");

  });
});
.nav {
  border: 1px;
  border-color: #2f70b1;
}

/* Additional CSS rules go here */

.notification-badge {
  /* Styles for notification badge */
}
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container-fluid">
  <br/>
  <div class="col-sm-10">
    <div class="panel with-nav-tabs panel-tab-block">
      <div class="panel-heading">
        <ul class="nav nav-tabs">
          <li class="active">
            <a data-toggle="tab" class="" href="#projects-tab">Projects</a>
          </li>
          <li>
            <a data-toggle="tab" id="forum-notification-badge" class="notification-badge" data-notification-badge="1" href="#forum-tab">Forum</a>
          </li>
        </ul>
      </div>
      <div class="panel-body">
        <div class="tab-content">
          <div id="projects-tab" class="tab-pane fade in active">
            <div class="row">

              <div class="col-sm-12">
                <h1>
                  Projects
                </h1>
              </div>
            </div>
            <div class="row">
              <div class="spacer-sml"></div>
            </div>
            <div class="row">
              <div class="spacer-sml"></div>
            </div>
          </div>
          <div id="forum-tab" class="tab-pane fade">
            <div class="tab-block-content forum-posts">
              <div class="">
                <div class="col-sm-12">
                  <h1>
                    Forum
                  </h1>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

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

What is the best way to incorporate spacing between elements in Selenium?

Utilizing Selenium, I attempted to extract data from a table on a webpage. The HTML structure is as follows: <table> <tbody> <tr> <td> <span>1</span> <span>0</span> ...

Implement modals using a for loop in Vue

I am facing an issue while trying to create modals for cards within loops. I attempted using v-bind:id="masa._id" and v-bind:data-target="'#'+masa._id", but the modal is not working. Can someone guide me on how to implement modals in loops? Belo ...

After the update to the page, the DOM retains the previous element

I'm currently developing a Chrome Extension (no prior knowledge needed for this inquiry...) and I have encountered an issue. Whenever I navigate to a page with a specific domain, a script is executed. This script simply retrieves the value of the attr ...

The Ajax post function behaves correctly in Chrome, however, no data is being received on the server when using Internet

Looking to fetch and manipulate an image (png) created by a flash application. When a user clicks on a link, this is what happens: var dataImgBase64 = document.getElementById("flashApp").getThumbnail(); The flash app then sends me the image in base64 for ...

Issue encountered while retrieving owner's screenname and URL from Dailymotion Data API

Encountered a problem when performing GET requests for both the PLAYLIST and VIDEO features. The values of owner.screenname and owner.url attributes are returned correctly when using GET on the console, but they show as undefined when the request is made v ...

Tips for showcasing a JSON Array in a table with AJAX on JSP

I'm relatively new to ajax and I am currently attempting to showcase data in a table within a JSP file. The API is being called using AJAX. The controller passes the following response: BatchwiseStudent [name=Ram, course=MCA (Commerce), <a href= ...

"Exploring the versatility of Tailwind css: A guide to implementing multiple themes

I know how to customize dark and light themes in tailwind, but I'm curious about extending this functionality to include additional themes such as pink, blue, red, etc. Is there a way to achieve this with regular tailwind CSS? <p className="t ...

Real-time calculation of dropdown options

Check out the demonstration here: http://jsfiddle.net/NaUAL/61/ <select name="one" id="one" > <option value="0" >Choose an option *</option> <option value="3000" >Plan A</option> <option ...

The JavascriptExecutor is unable to access the 'removeAttribute' property of a null object

While utilizing Javascript executor to remove the readonly attribute, I encountered an error message: Cannot read property 'removeAttribute' of null. I came across various discussions where users suggested that removing AdBlock from Chrome solve ...

Obtain the CSRF token from a webpage using JavaScript

I am currently working on a project where I need to retrieve the csrf token from a web page built with Django using javascript. The structure of my HTML template is as follows: <div class = "debugging"> <p id = "csrf">{% csrf_token %}</p ...

Tips for sending variables via ajax to the blade template in Laravel

When I select a team from the dropdown menu, I want to display a list of players who play for that team. I have included my code below, but I am unsure how to pass the variable from AJAX directly to Blade and then iterate through it using foreach. Any help ...

I am experiencing issues with my button not responding when I click on the top few pixels

I've created a StackBlitz version to illustrate the issue I'm facing. It seems like the problem might be related to my CSS for buttons... Essentially, when you click on the button at the very top few pixels, it doesn't register the click an ...

Storing a JavaScript array in a MySQL table using Ajax and PHP

https://i.sstatic.net/Ck84l.pngCurrently, my goal is to pass an array through AJAX to PHP and then save it in a table. The PHP script (running on a WAMP server) <?php $servername = "localhost"; $username = "root"; $password = "xx"; $dbname = "xx" ...

Experiencing Problems with Firefox Height?

Check out my current project on Codepen - it's a clock: http://codepen.io/www139/pen/MaxGyW Here's what it's supposed to look like: https://i.sstatic.net/tOMKh.png If you're using FireFox, you might notice that it looks different comp ...

Data from Ajax calls is only available upon refreshing the page

I am working on adding a notification button for inactive articles on my blog. I want to use AJAX so that the admin does not have to reload the page to view newly submitted inactive articles. I am trying to prepend HTML data to: <ul id="menu1" class= ...

Using jQuery UI autocomplete with special characters

Recently, I put together a simple jQuery example utilizing jQuery UI autocomplete feature. $(function() { //autocomplete $(".selector").autocomplete({ source: "getdata.php", minLength: 1 }); }) getdata.php: <?php if (isse ...

Web addresses and the presence of secure HTTP protocol?

Can someone confirm if it is true that when using https, URLs should begin with //[your-domain]/? A friend using WordPress mentioned this but I haven't been able to find specific information on the topic. ...

Retrieve information from the existing URL and utilize it as a parameter in an ajax request

Currently, I am working on a website with only one HTML page. The main functionality involves fetching the URL to extract an ID and then sending it to an API using an AJAX call. Upon success, the data related to the extracted ID is displayed on the page. H ...

List the @font-face URLs using JavaScript or jQuery

Can JavaScript or JQuery be used to list all the @font-face URLs (web font URLs) on a specific HTML page? ...

Suggestions missing in jQuery UI AutoComplete feature

My current issue involves dynamically creating an INPUT element and adding auto-complete functionality to it using the code below. However, there seems to be a problem with the .autocomplete line preventing the next line from executing properly. var avail ...