Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Add A Number To The Contents Of This Div

This is a discussion on Add A Number To The Contents Of This Div, within the Javascript section. This forum and the thread "Add A Number To The Contents Of This Div" are both part of the Frameworks category.

 
Reply to this topicStart new topic
> Add A Number To The Contents Of This Div
cosmicbdog
post Nov 19 2008, 08:46 AM
Post #1


Rapid Squeezer
****

Posts: 146
Joined: 3-July 08


Say you have a bunch of divs with numerals in them.

CODE
<div class="ticketNumber">1</div><div class="ticketNumber">2</div><div class="ticketNumber">3</div><div class="ticketNumber">4</div><div class="ticketNumber">5</div><div class="ticketNumber">6</div><div class="ticketNumber">7</div><div class="ticketNumber">8</div>


And you have a buttion
CODE
<div id='more'>More</div>


and you wanted to make that button add say '8' to each total.
CODE
        $j('#moreTickets').click(function(){
    
        $j('.ticketNumber').text($j(this).text() + 8);
        
        });
    

});


Thats how far I got. Obviously the 'this' I'm referring to refers to the #moreTickets just selected and not one of the divs with the numbers but thought I'd share to show what my head is trying to do.

Is it possible with jquery to simply add 10, or something to whatever is contained in the content of a bunch of classes?

This post has been edited by cosmicbdog: Nov 19 2008, 08:47 AM
Go to the top of the page
 
+Quote Post
c010depunkk
post Nov 21 2008, 12:10 AM
Post #2


Rapid Squeezer
Group Icon

Posts: 199
Joined: 14-February 08
From: Willich, Germany


CODE
$j('#moreTickets').click(function(){
   $j(this).text(parseInt($j(this).text())+8);
});

you almost had it right, I'd add a parseInt().
try this wink.gif


--------------------
www.c010depunkk.com ~ the hangout of a web developer
Go to the top of the page
 
+Quote Post
cosmicbdog
post Nov 21 2008, 02:24 AM
Post #3


Rapid Squeezer
****

Posts: 146
Joined: 3-July 08


Thanks for that! Yes now its actually adding up numbers, lol. It still doesn't quite work though because that $j(this) in there doesn't refer to the div in question.

I think what I need to do is do a for loop or something for each of the divs. So firstly do a count of the divs that are out on the page. Go through each div, work out the value of whats inside, then add a number.

Something like

CODE
$j('#moreTickets').click(function(){
        
// get number of options on the page
var ticketoptions = $j('.ticketNumber').size();

for (i = 1; i <=ticketoptions; i++) {

        // double the number of whats inside the div
        var newcount =  parseInt($j('.ticketNumber':eq(i)).text()) * 2;    

        // update the div
    $j('.ticketNumber':eq(i)).text(newcount);    

}
Go to the top of the page
 
+Quote Post
Rakuli
post Nov 21 2008, 02:33 AM
Post #4


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


jQuery has a built in foreach-type function you could use for this.

CODE
// Run a function on all the numbers in the div
$j('.ticketNumber').each(function()
{
     $(this).text(parseInt($(this).text()) + 8);
}


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
cosmicbdog
post Nov 21 2008, 02:44 AM
Post #5


Rapid Squeezer
****

Posts: 146
Joined: 3-July 08


The final 90% working function is like so
CODE
        $j('#moreTickets').click(function(){
                
        // get number of options on the page
        var ticketoptions = $j('.ticketNumber').size();
        

            for (i = 1; i <=ticketoptions; i++) {
        
                // double the number of whats inside the div
                var newcount =  parseInt($j('.ticketNumber:eq('+i+')').text()) + 14;    
        
                // update the div
                $j('.ticketNumber:eq('+i+')').text(newcount);    
        
            }


        });
Go to the top of the page
 
+Quote Post
cosmicbdog
post Nov 21 2008, 02:45 AM
Post #6


Rapid Squeezer
****

Posts: 146
Joined: 3-July 08


Oh... I didn't realise you posted!

Your function looks a little bit faster than mine, lol
Go to the top of the page
 
+Quote Post
Rakuli
post Nov 21 2008, 02:47 AM
Post #7


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


Does using each not achieve the same effect?

CODE
$j('#moreTickets').click(function(){
                
$j('.ticketNumber').each(function()
{
     $(this).text(parseInt($(this).text()) + 8);
});


        });



EDIT: LOL -- I had a syntax error in my first post anyway so the above code should be better wink.gif


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
cosmicbdog
post Nov 21 2008, 02:52 AM
Post #8


Rapid Squeezer
****

Posts: 146
Joined: 3-July 08


thats alright. master you have sharpened my sword and I found the issue myself wink.gif your function works much faster!
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new   16 Vanessa 408 27th February 2008 - 11:09 AM
Last post by: karinne
No New Posts   8 JustinStudios 347 29th March 2008 - 10:29 PM
Last post by: JustinStudios
No New Posts   11 MikeHopley 423 4th April 2008 - 06:37 AM
Last post by: MikeHopley
No New Posts   5 c010depunkk 190 6th April 2008 - 06:15 AM
Last post by: Simon
No New Posts   2 cosmicbdog 206 24th October 2008 - 05:35 AM
Last post by: cosmicbdog