Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Gianpietro Lavado
seal-mask
.a{fill-rule:evenodd;}techdegree
Gianpietro Lavado
Front End Web Development Techdegree Student 9,908 Points

Store jQuery method result to variable

Hi team,

I'm using a shoutcast plugin to retrieve listeners number from an internet radio an show it in a paragraph with id "p2".

$.SHOUTcast({
   host : 'doki.wavestreamer.com',
   port : 6607,
   stats : function(){
    $('#p2').text(this.get('currentlisteners'));
   }
}).startStats();

So the above function shows in p2 the current listeners for that specific radio. Now I would like to store that information in a java variable to be able to perform some math with it. How could I achieve this?

Thanks! Gianpietro

3 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

This might work for you:

var listeners; 
$.SHOUTcast({
   host : 'doki.wavestreamer.com',
   port : 6607,
   stats : function(){
    listeners = this.get('currentlisteners');
    $('#p2').text(listeners);
   }
}).startStats();
Gareth Borcherds
Gareth Borcherds
9,372 Points

I'm not familiar with how Java interacts with browsers, but I believe Java is server side just like php is. You would need to send the variable to your server so you can handle it there. You would accomplish this by using in jQuery either $.ajax, $.get, or $.post

I actually just answered another one similar to this for php here: https://teamtreehouse.com/forum/variable-from-jquery-to-php

That example should give you an idea of how you would do it. Hope it helps.

Gianpietro Lavado
seal-mask
.a{fill-rule:evenodd;}techdegree
Gianpietro Lavado
Front End Web Development Techdegree Student 9,908 Points

Gareth, Dave, thanks a million for taking time to answer. Finally I used the code suggested by Dave and accomplished the goal. Thanks again! Gianpietro