echo a javascript variable
Asked Answered
G

3

6

What is the best way to echo the variable get_parestotal?

Some help please! Thank you

  <script type="text/javascript">
 $(document).ready(function(){
                    var get_parestotal = 0
                $(".parestotal").each(function(){
                    get_parestotal += parseFloat($(this).text());
                });
                alert(get_parestotal);

            });

</script>



<? echo json_encode($get_parestotal); ?>
Genuflect answered 22/9, 2014 at 12:41 Comment(7)
You can't do that. But you can alert or console.log(get_parestotal)Tempa
the echo is in PHP server side, while the variable is on the client side...Schoolfellow
Right. Can you linking me some example using the console.log ? thanksGenuflect
developer.chrome.com/devtools/docs/consoleTrimurti
You want to output the info to the page? Than console.log is not what you want. Learn about innerHTML or appendChild.Cerebrospinal
Where do you want to display this value? If you want to show the value somewhere on the page then you'd need to identify the HTML element which should contain the value and set it as the content of that element. There is no echo in JavaScript.Adjuvant
Thanks guys I need to display this value on a specific div. Cann you give me a hint on innerHTML implementation for this issue?Genuflect
D
7

You can log variables and so on in the console.

e.g.:

console.log(get_parestotal);

You can even concatenate witht he use of + console.log('This is your variable'+get_parestotal)

You can even add styling to your log (color, background-color,...):

console.log('%cThis is your variable'+get_parestotal+'!','color:green;');

There are some alternatives to console.log() you could use:

 console.warn();
 console.info();
 console.error();
 console.debug();
Doucette answered 22/9, 2014 at 12:54 Comment(2)
You can also just use multiple arguments, ie console.log('bla', window, 123), if you use a good browser you can work with the output much better than as a string puu.sh/bJgmN/13103a5940.pngCliffhanger
Thanks guys I not properly explained the problem above. I need to display the value on a div.Genuflect
S
0

You can either do alert(myvar) or console.log(myvar).

Caveat: console.log is only supported in certain browsers...see the link.

Sunk answered 22/9, 2014 at 13:0 Comment(0)
I
0

if you want a console output than use console.log(get_parestotal);

Inglenook answered 22/9, 2014 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.