jquery for calculating spans

In this scenario, I am attempting to calculate values based on the active span element. My complete code is available on JSFiddle. The span is not editable and I have assumed that the span values are 40. If a user selects a span, it will change to green color. If another span is clicked, the value of span#returndata should be incremented by 40 (i.e., 40 + 40 = 80). With each subsequent click on a span, the total should increment by 40 (e.g., 40 + 40 + 40 = 120).

Below is the jQuery code snippet I have tried, but so far without success:

JsFiddle

jQuery

$(".text").click(function(){
    $(this).toggleClass('selected');
    $(function(){
        $('span#text').click(function(){
            var value = 40;
            var t = parseInt($(this).text());
            var total = value * t;
            $('#returndata').text(total);
        });
    });
});

Answer №1

I made some updates to your jFiddle in order to get it working properly. Check out the revised version here!

It's important to recalculate the value every time a user clicks, based on the number of seats chosen. This will prevent any inconsistencies from occurring.

Here is the updated JS code:

$(".text").click(function(){
    $(this).toggleClass('selected');
    var count = $('.selected').length;
    var value = 40;
    $('.returndata').text(value*count);
});

Answer №2

Give this a shot

$(".text").on("click", function(){
    $(this).toggleClass('selected');
    var value = 40;
    var t = parseInt($(this).text(), 10);
    var total = value + t;
    $('span.returndata').text(total);   
})

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

Leveraging the power of font awesome icons as background images within React applications

I am facing an issue with my dropdown menu. I have styled the caret using CSS background-image property, but now I would like to use a Font Awesome icon instead. I attempted to implement two different styles for the background image with no success. The d ...

Sliding background in a multi-column accordion

I've hit a roadblock with this project. I'm working on setting up a FAQ section that needs to display its items in two columns on desktop, each with a drop shadow effect using Bootstrap 4. I've experimented with Flexbox and CSS columns with ...

Having trouble getting Django to display images using jQuery? Uncaught Reference Error may be preventing it

Currently, I am experimenting with implementing a jquery example on my django-based website. As I am still in the learning phase, I kindly ask for your patience. This is a snippet of what my code looks like at this point: {% extends "base.html" %} {% loa ...

Loop through an array of buttons using an event listener

My HTML and Javascript code currently have the functionality to show/hide a row of 3 images upon button click. However, I need this feature to work for two additional rows similar to this one. I believe it involves looping through an array of buttons, but ...

Exploring the Dynamic Resizing of Geometry Meshes in three.js

Is there a way to adjust the height of my geometry meshes dynamically? You can check out my demo here. ...

Centering an icon vertically and introducing animation upon hovering over an image

I'm having difficulty with vertically centering an icon that drops down on image hover, and I want to make its transition smoother. Currently, it's dropping down too abruptly and no matter what transition property I apply (ease, ease-out, etc.), ...

Is it possible to display the command output in Grunt as it runs?

When I run this command, I am not seeing any output and it runs endlessly: grunt.registerTask('serve', function () { var done = this.async(); grunt.util.spawn({ cmd: 'jekyll', args: ['serve'], ...

Inefficient handling of multiple ajax requests leading to delays in response

In my two-column user interface, I am displaying news from two separate AJAX calls using jQuery. In addition to these, there are other AJAX calls for various elements on the page. Both of these services are cached on the server side and execute in less th ...

excess white space appears in the mobile version

I recently completed a website using both materializecss and bootstrap platforms. While I know this may not be the best practice, it worked for my needs. However, I am facing an issue with the mobile view. When I reduce the viewport size, a margin appear ...

Which npm module is recommended for testing POST requests in unit testing?

One essential feature is the ability to specify the req.params for a POST request. The documentation for the supertest package does not clearly outline how to accomplish this. ...

Link inserted in between spaces

Working with Eloqua. Any ideas on what might be causing a space to appear in a link? The space doesn't show up during testing, only in the live version. Here is the code snippet: <p style="line-height:18px;"> <font face="calibri, Arial, H ...

Retrieving information from jQuery object using jQuery

I am utilizing ajax to retrieve information from the database. Once I have retrieved the information from the table, I am using json_encode to format the data into JSON. Then, I am utilizing parseJSON to display the data in JavaScript. After receiving the ...

A guide on showcasing a blob type in a struts2 jquery grid sourced from a mysql database

I managed to successfully save an image as a blob type in MySQL and now I'm looking to showcase it using the Struts2 jQuery grid widget. However, when I tried using edittype="image", it only displayed some random string instead of the actual image. Ca ...

Incorporate an element using addEventListener and retrieve it in the following loop

When I add an event listener to all HTML elements with the class "card-title", it successfully creates new elements with the same class when clicked. However, these newly created elements do not respond to the event listener. I suspect this is because th ...

Bootstrap - Custom CSS styles applied to specific grid prefixes

I am currently working on developing a mobile site using bootstrap and I have a need to apply different CSS rules based on the grid prefix: col-xs, col-sm Here is a snippet of the HTML code: <header class="col-xs col-sm"> Hi </header> For ...

Utilizing the onscroll feature to trigger a function within a ScrollView

I am facing an issue with an animated view where I need to execute multiple events within the onScroll property. The current onScroll implementation is as follows: onScroll={ Animated.event( [{ nativeEvent: { conten ...

Maximizing the effectiveness of utilizing the include function for displaying various external pages within a single <div> element

Would it be more efficient to utilize the include method for displaying various external pages within a single <div>? When I say "more efficient," I am referring to the speed of loading the pages and its effectiveness compared to using ajax and javas ...

Mastering the Art of Customizing Styling in Ant Design

I'm currently working with a table from Ant Design, but I'm facing an issue where the padding or margin applied by the Ant Design CSS is preventing the table from expanding on the left side. The table has an inline CSS of table layout: auto which ...

Using JavaScript to generate a span element within a div container

This snippet of HTML achieves the desired result by displaying category: followed by a value passed in JavaScript. <div id="category-order-id" class="additional-order-info"> <!-- <span class="additional-order-info-gr ...

Form-select failing to transmit data

There seems to be an issue with one specific HTML field not sending data. This is my HTML code: <form id="login_registro_form" role="form" method="post" action="./controllers/register.php"> <div cl ...